-
Notifications
You must be signed in to change notification settings - Fork 12
/
sm_realloc_i.c
128 lines (117 loc) · 3.21 KB
/
sm_realloc_i.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
/*
* This file is a part of SMalloc.
* SMalloc is MIT licensed.
* Copyright (c) 2017 Andrey Rys.
*/
#include "smalloc_i.h"
/*
* Please do NOT use this function directly or rely on it's presence.
* It may go away in future SMalloc versions, or it's calling
* signature may change. It is internal function, hence "_i" suffix.
*/
void *sm_realloc_pool_i(struct smalloc_pool *spool, void *p, size_t n, int nomove)
{
struct smalloc_hdr *basehdr, *shdr, *dhdr;
void *r;
char *s;
int found;
size_t rsz, usz, x;
uintptr_t tag;
if (!smalloc_verify_pool(spool)) {
errno = EINVAL;
return NULL;
}
if (!p) return sm_malloc_pool(spool, n);
if (!n && p) {
sm_free_pool(spool, p);
return NULL;
}
/* determine user size */
shdr = USER_TO_HEADER(p);
if (!smalloc_is_alloc(spool, shdr)) smalloc_UB(spool, p);
usz = shdr->usz;
rsz = shdr->rsz;
/* newsize is lesser than allocated - truncate */
if (n <= usz) {
if (spool->do_zero) memset((char *)p + n, 0, shdr->rsz - n);
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += usz;
memset(s, 0, HEADER_SZ);
if (spool->do_zero) memset(s+HEADER_SZ, 0, rsz - usz);
shdr->rsz = (n%HEADER_SZ)?(((n/HEADER_SZ)+1)*HEADER_SZ):n;
shdr->usz = n;
shdr->tag = tag = smalloc_mktag(shdr);
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += shdr->usz;
for (x = 0; x < sizeof(struct smalloc_hdr); x += sizeof(uintptr_t)) {
tag = smalloc_uinthash(tag);
memcpy(s+x, &tag, sizeof(uintptr_t));
}
memset(s+x, 0xff, shdr->rsz - shdr->usz);
return p;
}
/* newsize is bigger than allocated, but there is free room - modify */
if (n > usz && n <= rsz) {
if (spool->do_zero) {
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += usz;
memset(s, 0, HEADER_SZ);
}
shdr->usz = n;
shdr->tag = tag = smalloc_mktag(shdr);
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += shdr->usz;
for (x = 0; x < sizeof(struct smalloc_hdr); x += sizeof(uintptr_t)) {
tag = smalloc_uinthash(tag);
memcpy(s+x, &tag, sizeof(uintptr_t));
}
memset(s+x, 0xff, shdr->rsz - shdr->usz);
return p;
}
/* newsize is bigger, larger than rsz but there are free blocks beyond - extend */
basehdr = (struct smalloc_hdr *)spool->pool; dhdr = shdr+(rsz/HEADER_SZ); found = 0;
while (CHAR_PTR(dhdr)-CHAR_PTR(basehdr) < spool->pool_size) {
x = CHAR_PTR(dhdr)-CHAR_PTR(shdr);
if (smalloc_is_alloc(spool, dhdr))
goto allocblock;
if (n + HEADER_SZ <= x) {
x -= HEADER_SZ;
found = 1;
goto outfound;
}
dhdr++;
}
outfound:
/* write new numbers of same allocation */
if (found) {
if (spool->do_zero) {
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += usz;
memset(s, 0, HEADER_SZ);
memset(s+HEADER_SZ, 0, rsz - usz);
}
shdr->rsz = x;
shdr->usz = n;
shdr->tag = tag = smalloc_mktag(shdr);
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += shdr->usz;
for (x = 0; x < sizeof(struct smalloc_hdr); x += sizeof(uintptr_t)) {
tag = smalloc_uinthash(tag);
memcpy(s+x, &tag, sizeof(uintptr_t));
}
memset(s+x, 0xff, shdr->rsz - shdr->usz);
return p;
}
allocblock:
/* newsize is bigger than allocated and no free space - move */
if (nomove) {
/* fail if user asked */
errno = ERANGE;
return NULL;
}
r = sm_malloc_pool(spool, n);
if (!r) return NULL;
memcpy(r, p, usz);
sm_free_pool(spool, p);
return r;
}