xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/dyndb/driver/util.h (revision 06dfa8449cb5e76c0044ec0f3badf7d5180af0f5)
1 /*	$NetBSD: util.h,v 1.4 2022/09/23 12:15:25 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 #ifndef _LD_UTIL_H_
37 #define _LD_UTIL_H_
38 
39 #include <isc/mem.h>
40 
41 #include <dns/types.h>
42 
43 #include "log.h"
44 
45 #define CLEANUP_WITH(result_code)       \
46 	do {                            \
47 		result = (result_code); \
48 		goto cleanup;           \
49 	} while (0)
50 
51 #define CHECK(op)                            \
52 	do {                                 \
53 		result = (op);               \
54 		if (result != ISC_R_SUCCESS) \
55 			goto cleanup;        \
56 	} while (0)
57 
58 #define CHECKED_MEM_GET(m, target_ptr, s)                      \
59 	do {                                                   \
60 		(target_ptr) = isc_mem_get((m), (s));          \
61 		if ((target_ptr) == NULL) {                    \
62 			result = ISC_R_NOMEMORY;               \
63 			log_error("Memory allocation failed"); \
64 			goto cleanup;                          \
65 		}                                              \
66 	} while (0)
67 
68 #define CHECKED_MEM_GET_PTR(m, target_ptr) \
69 	CHECKED_MEM_GET(m, target_ptr, sizeof(*(target_ptr)))
70 
71 #define CHECKED_MEM_STRDUP(m, source, target)                  \
72 	do {                                                   \
73 		(target) = isc_mem_strdup((m), (source));      \
74 		if ((target) == NULL) {                        \
75 			result = ISC_R_NOMEMORY;               \
76 			log_error("Memory allocation failed"); \
77 			goto cleanup;                          \
78 		}                                              \
79 	} while (0)
80 
81 #define ZERO_PTR(ptr) memset((ptr), 0, sizeof(*(ptr)))
82 
83 #define MEM_PUT_AND_DETACH(target_ptr)                        \
84 	isc_mem_putanddetach(&(target_ptr)->mctx, target_ptr, \
85 			     sizeof(*(target_ptr)))
86 
87 #endif /* !_LD_UTIL_H_ */
88