summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/alloc.c
blob: f981906e0dcaea071281f7852233611e39c1e2f9 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>

struct chunkControl {
	size_t nodeCount;
	size_t chunkSize;
};

struct nodeControl {
	struct chunkControl *chunk;
	size_t nodeSize;
};

#define ROUND_UP_LENGTH(len) ((len+7) & ~0x07)

extern struct nodeControl *mallocNextNode;

#ifdef L_malloc
/* This variable is a pointer to the next place to allocate from.
 * Note: This variable makes the code NOT thread save. */
struct nodeControl *mallocNextNode = 0;
static size_t PageSize = 0;

#endif

#ifdef L_calloc_dbg

void *
calloc_dbg(size_t num, size_t size, char * function, char * file, int line)
{
	void * ptr;
	fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num*size, function, file, line);
	ptr = calloc(num,size);
	fprintf(stderr, "%p\n", ptr);
	return ptr;
}

#endif

#ifdef L_malloc_dbg

void *
malloc_dbg(size_t len, char * function, char * file, int line)
{
	void * result;
	fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, file, line);
	result = malloc(len);
	fprintf(stderr, "%p\n", result);    
	return result;
}

#endif

#ifdef L_free_dbg

void
free_dbg(void * ptr, char * function, char * file, int line)
{
	fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, line);
  	free(ptr);
}

#endif


#ifdef L_calloc

void *
calloc(size_t num, size_t size)
{
	void * ptr = malloc(num*size);
	if (ptr)
		memset(ptr, 0, num*size);
	return ptr;
}

#endif

#ifdef L_malloc

void *
malloc(size_t len)
{
	void *result;
	struct chunkControl *chunk;
	struct nodeControl  *next;
	size_t size;

	/* round len up to keep things on even boundaries */
	len = ROUND_UP_LENGTH(len);

	if (len == 0)
		return 0;

TryAgain:
	if (mallocNextNode != 0) {
		/* first see if this request will fit on this chunk */
		next  = mallocNextNode;
		chunk = next->chunk;
		if (((char *)next + sizeof(struct nodeControl)*2 + len) < 
		    ((char *)chunk + chunk->chunkSize))
		{
			/* this request will fit, so simply move the next
			 * pointer ahead and update chunk node count */
			next->nodeSize = len;
			result = (char *)next + sizeof(struct nodeControl);
			chunk->nodeCount++;
			next = (struct nodeControl *)
			          ((char *)next + (sizeof(struct nodeControl) + len));
			next->chunk = chunk;
			next->nodeSize = 0;
			mallocNextNode = next;

			return result; /* normal return path */
		}
		
	}
	
	/* the request will not fit on this chunk, so get another chunk */
	if (PageSize == 0) {
		PageSize = getpagesize();
	}
	size = len + (sizeof(struct chunkControl) + (sizeof(struct nodeControl) * 2));
	if (size < PageSize * 2) {
		size = PageSize * 2;
	}
	size = (size + (PageSize-1)) & ~(PageSize-1);

	chunk = mmap((void *)0, size, PROT_READ | PROT_WRITE,
	              MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
	if (chunk == (void*)-1)
		return 0;

	chunk->chunkSize = size;
	chunk->nodeCount = 0;
	next             = (struct nodeControl *)
		                 ((char *)chunk + sizeof(struct chunkControl));
	next->chunk      = chunk;
	mallocNextNode   = next;

	goto TryAgain;
}

#endif

#ifdef L_free

void
free(void * ptr)
{
	struct chunkControl *chunk;
	struct nodeControl  *node;
	
	if (ptr == 0) {
		return;
	}
	/* get a pointer to the control information for this memory node
	 * and the chunk it belongs to */
	node  = (struct nodeControl *)ptr - 1;
	chunk = node->chunk;
	/* decrement the node count and if it is zero free the chunk */
	chunk->nodeCount--;
	if (chunk->nodeCount == 0) {
		if ((void *)mallocNextNode >= (void *)chunk && 
		     ((void *)mallocNextNode < (void *)((char *)chunk + chunk->chunkSize)))
		{
			mallocNextNode = 0;
		}
		munmap(chunk, chunk->chunkSize);
	}
}

#endif

#ifdef L_realloc

void *
realloc(void *ptr, size_t len)
{
	struct nodeControl *node;
	size_t oldSize;
	void *new;
	
	
	if (ptr == 0) {
		return malloc(len);
	}
	if (len == 0) {
		free(ptr);
		return 0;
	}
	node    = (struct nodeControl *)ptr - 1;
	oldSize = node->nodeSize;
	if (oldSize >= len) {
		return ptr;
	}
	
	new = malloc(len);
	memcpy(new, ptr, len);
	free(ptr);
	return new;
}

#endif