SURFEX v8.1
General documentation of Surfex
memory_hook.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <stdio.h>
5 
6 #ifdef LINUX
7 
8 /*
9  *
10  * Philippe Marguinaud, Meteo-France
11  *
12  */
13 
14 
15 static size_t align = 0; /* Must be a multiple of sizeof (void) */
16 static unsigned char snan8[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x7f };
17 static unsigned char cinit[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
18 static unsigned char zero0[] = { 0x00 };
19 static unsigned char * init = NULL;
20 static int sizeof_init = 0;
21 static int count = -1;
22 
23 void * __wrap_malloc (size_t size, const void *caller)
24 {
25  void * ptr;
26  int _align = align > 0 ? align : sizeof (void*);
27  int c;
28 
29  if ((c = posix_memalign (&ptr, _align, size)) != 0)
30  {
31  ptr = NULL;
32  printf (" c = %d, EINVAL = %d, ENOMEM = %d, align = %d, size = %d\n",
33  c, EINVAL, ENOMEM, align, size);
34  }
35 
36  if ((init != NULL) && (ptr != NULL))
37  {
38  unsigned char * c;
39  int i;
40  for (i = 0, c = ptr; i < size; i++)
41  c[i] = init[i%sizeof_init];
42  }
43 
44  if (count >= 0)
45  __sync_fetch_and_add (&count, 1);
46 
47  return ptr;
48 }
49 
50 void __attribute__((constructor))
51 memory_hook_init_ ()
52 {
53  const char * MEMORY_HOOK_ALIGN = getenv ("MEMORY_HOOK_ALIGN");
54  const char * MEMORY_HOOK_INIT = getenv ("MEMORY_HOOK_INIT");
55  const char * MEMORY_HOOK_COUNT = getenv ("MEMORY_HOOK_COUNT");
56  if (MEMORY_HOOK_INIT)
57  {
58  if (strcasecmp (MEMORY_HOOK_INIT, "NAN") == 0)
59  {
60  init = &snan8[0];
61  sizeof_init = sizeof (snan8);
62  }
63  else if (strcasecmp (MEMORY_HOOK_INIT, "ZERO") == 0)
64  {
65  init = &zero0[0];
66  sizeof_init = sizeof (zero0);
67  }
68  else if (strncasecmp (MEMORY_HOOK_INIT, "0X", 2) == 0)
69  {
70  long long unsigned int x = 0;
71  const char * c;
72  for (c = MEMORY_HOOK_INIT+2; *c; c++)
73  if (('0' <= *c) && (*c <= '9'))
74  x = 16 * x + (*c - '0');
75  else if (('a' <= *c) && (*c <= 'f'))
76  x = 16 * x + (*c - 'a' + 10);
77  else if (('A' <= *c) && (*c <= 'F'))
78  x = 16 * x + (*c - 'A' + 10);
79  else
80  break;
81  init = &cinit[0];
82  memcpy (cinit, &x, sizeof (x));
83  sizeof_init = sizeof (cinit);
84  }
85  printf (" MEMORY_HOOK_INIT = %s\n", MEMORY_HOOK_INIT);
86  }
87  if (MEMORY_HOOK_ALIGN)
88  align = atoi (MEMORY_HOOK_ALIGN);
89  if (MEMORY_HOOK_COUNT)
90  count = 0;
91 }
92 
93 void __attribute__((destructor))
94 memory_hook_exit_ ()
95 {
96  if (count > 0)
97  printf ("MEMORY_HOOK_COUNT = %d\n", count);
98 }
99 
100 #endif
101 
102 
static long size
Definition: bytes_io.c:262
static unsigned char snan8[]
Definition: memory_hook.c:16
static unsigned char * init
Definition: memory_hook.c:19
static unsigned char cinit[]
Definition: memory_hook.c:17
static int sizeof_init
Definition: memory_hook.c:20
void __attribute__((constructor))
Definition: memory_hook.c:50
static unsigned char zero0[]
Definition: memory_hook.c:18
static size_t align
Definition: memory_hook.c:15
void * __wrap_malloc(size_t size, const void *caller)
Definition: memory_hook.c:23
static int count
Definition: memory_hook.c:21