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