BRL-CAD
badmagic.c
Go to the documentation of this file.
1 /* B A D M A G I C . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include "bio.h"
24 
25 #include "bu/log.h"
26 #include "bu/magic.h"
27 
28 #define MAGICBUFSIZ 512
29 
30 void
31 bu_badmagic(const uint32_t *ptr, uint32_t magic, const char *str, const char *file, int line)
32 {
33  char buf[MAGICBUFSIZ] = {'\0'};
34 
35  if (UNLIKELY(!(ptr))) {
36  snprintf(buf, MAGICBUFSIZ, "ERROR: NULL %s pointer, file %s, line %d\n",
37  str, file, line);
38  } else if (UNLIKELY(((size_t)(ptr)) & (sizeof(uint32_t)-1))) {
39  snprintf(buf, MAGICBUFSIZ, "ERROR: %p mis-aligned %s pointer, file %s, line %d\n",
40  (void *)ptr, str, file, line);
41  } else if (UNLIKELY(*(ptr) != (uint32_t)(magic))) {
42  snprintf(buf, MAGICBUFSIZ, "ERROR: bad pointer %p: s/b %s(x%lx), was %s(x%lx), file %s, line %d\n",
43  (void *)ptr,
44  str, (unsigned long)magic,
45  bu_identify_magic(*(ptr)), (unsigned long)*(ptr),
46  file, line);
47  }
48  if (UNLIKELY(buf[0] != '\0'))
49  bu_bomb(buf);
50 }
51 
52 /*
53  * Local Variables:
54  * mode: C
55  * tab-width: 8
56  * indent-tabs-mode: t
57  * c-file-style: "stroustrup"
58  * End:
59  * ex: shiftwidth=4 tabstop=8
60  */
Header file for the BRL-CAD common definitions.
oldeumate l2 magic
Definition: nmg_mod.c:3843
void bu_badmagic(const uint32_t *ptr, uint32_t magic, const char *str, const char *file, int line)
Definition: badmagic.c:31
#define MAGICBUFSIZ
Definition: badmagic.c:28
void bu_bomb(const char *str) _BU_ATTR_NORETURN
Definition: bomb.c:91
const char * bu_identify_magic(uint32_t magic)
#define UNLIKELY(expression)
Definition: common.h:282