1/*	$NetBSD: util.h,v 1.5 2024/02/21 22:51:27 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0 AND ISC
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*
17 * Copyright (C) Red Hat
18 *
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS" AND AUTHORS DISCLAIMS ALL WARRANTIES WITH
24 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
26 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
27 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
28 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
29 * PERFORMANCE OF THIS SOFTWARE.
30 */
31
32/*
33 * Memory allocation and error handling utilities.
34 */
35
36#pragma once
37
38#include <isc/mem.h>
39
40#include <dns/types.h>
41
42#include "log.h"
43
44#define CLEANUP_WITH(result_code)       \
45	do {                            \
46		result = (result_code); \
47		goto cleanup;           \
48	} while (0)
49
50#define CHECK(op)                            \
51	do {                                 \
52		result = (op);               \
53		if (result != ISC_R_SUCCESS) \
54			goto cleanup;        \
55	} while (0)
56
57#define CHECKED_MEM_GET(m, target_ptr, s)                      \
58	do {                                                   \
59		(target_ptr) = isc_mem_get((m), (s));          \
60		if ((target_ptr) == NULL) {                    \
61			result = ISC_R_NOMEMORY;               \
62			log_error("Memory allocation failed"); \
63			goto cleanup;                          \
64		}                                              \
65	} while (0)
66
67#define CHECKED_MEM_GET_PTR(m, target_ptr) \
68	CHECKED_MEM_GET(m, target_ptr, sizeof(*(target_ptr)))
69
70#define CHECKED_MEM_STRDUP(m, source, target)                  \
71	do {                                                   \
72		(target) = isc_mem_strdup((m), (source));      \
73		if ((target) == NULL) {                        \
74			result = ISC_R_NOMEMORY;               \
75			log_error("Memory allocation failed"); \
76			goto cleanup;                          \
77		}                                              \
78	} while (0)
79
80#define ZERO_PTR(ptr) memset((ptr), 0, sizeof(*(ptr)))
81
82#define MEM_PUT_AND_DETACH(target_ptr)                        \
83	isc_mem_putanddetach(&(target_ptr)->mctx, target_ptr, \
84			     sizeof(*(target_ptr)))
85