-
Notifications
You must be signed in to change notification settings - Fork 2
/
zutil.c
167 lines (147 loc) · 3.97 KB
/
zutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-
* Copyright (c) 2007 TANDBERG Telecom AS
* Copyright (c) 2008-2009 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Dag-Erling Smørgrav <[email protected]>
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zutil.h"
#define ZMAGIC 0x5254505a
#define ZALIGN (sizeof(void *))
struct zheader {
uint32_t zmagic;
size_t zlen;
size_t zalign;
void *zptr;
char zdata[];
};
/*
* On modern systems with memory overcommit, malloc() will not fail when
* physical memory runs out, so checking its return value is mostly
* pointless. However, if malloc() should return NULL, the subsequent
* assignment to zh->zmagic will cause a segfault, just like a real
* out-of-memory condition.
*/
void *
Zalloc(size_t len, size_t align)
{
struct zheader *zh;
void *zptr;
zh = zptr = malloc(sizeof *zh + len + align);
zassert(((uintptr_t)zh % ZALIGN) == 0);
if (align) {
zassert((align % ZALIGN) == 0);
while ((uintptr_t)zh->zdata % align)
zh = (struct zheader *)((char *)zh + ZALIGN);
}
zh->zmagic = ZMAGIC;
zh->zlen = len;
zh->zalign = align;
zh->zptr = zptr;
memset(zh->zdata, 0, len);
zassert(align == 0 || ((uintptr_t)zh->zdata % align) == 0);
return (zh->zdata);
}
void *
Zrealloc(void *ptr, size_t len)
{
struct zheader *zh = NULL;
size_t olen = 0;
if (ptr) {
zh = (struct zheader *)ptr - 1;
zassert(zh->zmagic == ZMAGIC);
zassert(zh->zalign == 0);
zassert(zh->zptr == zh);
if (len < zh->zlen)
memset(zh->zdata + len, 0, zh->zlen - len);
olen = zh->zlen;
}
zh = realloc(zh, sizeof *zh + len);
zh->zmagic = ZMAGIC;
if (len > olen)
memset(zh->zdata + olen, 0, len - olen);
zh->zlen = len;
zh->zalign = 0;
zh->zptr = zh;
return (zh->zdata);
}
void
Zfree(void *ptr, size_t len)
{
struct zheader *zh;
if (!ptr) {
zassert(len == 0);
return;
}
zh = (struct zheader *)ptr - 1;
zassert(zh->zmagic == ZMAGIC);
zassert(len == 0 || len == zh->zlen);
memset(zh->zdata, 0, zh->zlen);
free(zh->zptr);
}
char *
Zstrdup(const char *str)
{
char *dup;
size_t len;
zassert(str != NULL);
len = strlen(str);
dup = Zalloc(len + 1, 0);
memcpy(dup, str, len + 1);
return (dup);
}
void *
Zmemdup(const void *buf, size_t len)
{
char *dup;
zassert(buf != NULL);
zassert(len != 0);
dup = Zalloc(len, 0);
memcpy(dup, buf, len);
return (dup);
}
void
Zassert(const char *file, int line, const char *func, const char *cond)
{
fprintf(stderr, "assertion failed in %s() on %s:%d: %s\n",
func, file, line, cond);
abort();
}
void
Zunreach(const char *file, int line, const char *func)
{
fprintf(stderr, "unreachable code reached in %s() on %s:%d\n",
func, file, line);
abort();
}