Update m4/ax_pthread.m4.
[libpwmd.git] / src / mem.c
blob2a98a209e0f3cb47896b408bab98d011ff266258
1 /*
2 Copyright (C) 2016, 2017 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but 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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 USA
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stddef.h>
31 #include "mem.h"
33 void *
34 _xrealloc_gpgrt (void *p, size_t n)
36 if (!n)
38 _xfree (p);
39 return NULL;
42 if (!p)
43 return _xmalloc (n);
45 return _xrealloc (p, n);
48 #ifndef MEM_DEBUG
50 struct memchunk_s
52 size_t size;
53 char data[1];
56 void
57 _xfree (void *ptr)
59 struct memchunk_s *m;
60 void *p;
62 if (!ptr)
63 return;
65 m = (struct memchunk_s *)((char *)ptr-(offsetof (struct memchunk_s, data)));
66 p = (void *)((char *)m+(offsetof (struct memchunk_s, data)));
67 wipememory (p, 0, m->size);
68 free (m);
71 void *
72 _xmalloc (size_t size)
74 struct memchunk_s *m;
76 if (!size)
77 return NULL;
79 m = malloc (sizeof (struct memchunk_s)+size);
80 if (!m)
81 return NULL;
83 m->size = size;
84 return (void *)((char *)m+(offsetof (struct memchunk_s, data)));
87 void *
88 _xcalloc (size_t nmemb, size_t size)
90 void *p;
91 struct memchunk_s *m;
93 m = malloc (sizeof (struct memchunk_s)+(nmemb*size));
94 if (!m)
95 return NULL;
97 m->size = nmemb*size;
98 p = (void *)((char *)m+(offsetof (struct memchunk_s, data)));
99 memset (p, 0, m->size);
100 return p;
103 void *
104 _xrealloc (void *ptr, size_t size)
106 void *p, *np;
107 struct memchunk_s *m, *mp;
108 size_t n;
110 if (!size && ptr)
112 m = (struct memchunk_s *)((char *)ptr-(offsetof (struct memchunk_s, data)));
113 p = (void *)((char *)m+(offsetof (struct memchunk_s, data)));
114 wipememory (p, 0, m->size);
115 free (m);
116 return NULL;
118 else if (!ptr)
119 return _xmalloc (size);
121 m = malloc (sizeof (struct memchunk_s)+size);
122 if (!m)
123 return NULL;
125 m->size = size;
126 np = (void *)((char *)m+(offsetof (struct memchunk_s, data)));
128 mp = (struct memchunk_s *)((char *)ptr-(offsetof (struct memchunk_s, data)));
129 p = (void *)((char *)mp+(offsetof (struct memchunk_s, data)));
131 n = size > mp->size ? mp->size : size;
132 memcpy (np, p, n);
133 wipememory (p, 0, mp->size);
135 free (mp);
136 return (void *)((char *)m+(offsetof (struct memchunk_s, data)));
138 #endif