xref: /netbsd-src/external/mpl/bind/dist/lib/isc/include/isc/magic.h (revision 782713e6c126f1866c6d9cfdee4ceb49483b5828)
1 /*	$NetBSD: magic.h,v 1.6 2023/01/25 21:43:31 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
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 #ifndef ISC_MAGIC_H
17 #define ISC_MAGIC_H 1
18 
19 #include <isc/likely.h>
20 
21 /*! \file isc/magic.h */
22 
23 typedef struct {
24 	unsigned int magic;
25 } isc__magic_t;
26 
27 /*%
28  * To use this macro the magic number MUST be the first thing in the
29  * structure, and MUST be of type "unsigned int".
30  * The intent of this is to allow magic numbers to be checked even though
31  * the object is otherwise opaque.
32  */
33 static __inline int
34 checkmagic(const void *_p, unsigned int _m)
35 {
36 	const isc__magic_t *s = _p;
37 	return s->magic == _m;
38 }
39 
40 #define ISC_MAGIC_VALID(a, b)       \
41 	(ISC_LIKELY((a) != NULL) && ISC_LIKELY(checkmagic(a, b))) \
42 
43 #define ISC_MAGIC(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
44 
45 #endif /* ISC_MAGIC_H */
46