-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathallocate.c
63 lines (52 loc) · 962 Bytes
/
allocate.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
/*
* Ridiculous heap routines
*/
#include <assert.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include "c2ada.h"
#undef NULL
#define NULL 0
char* new_string(char* str)
{
char* result;
int len;
assert(str != NULL);
len = strlen(str);
assert(len >= 0);
result = (char*)malloc(len + 1);
assert(result != NULL);
strcpy(result, str);
return result;
}
#define BUFSIZE 1024
char* new_strf(char* str, ...)
{
va_list ap;
int n;
char buf[BUFSIZE];
va_start(ap, str);
n = vsprintf(buf, str, ap);
va_end(ap);
if(n >= BUFSIZE)
{
fatal(__FILE__, __LINE__, "buffer overrun in new_strf");
}
return new_string(buf);
}
void* allocate(size_t size)
{
void* ptr;
assert(size > 0);
ptr = malloc(size);
assert(ptr != 0);
memset(ptr, 0, size);
return ptr;
}
void deallocate(void* ptr)
{
free(ptr);
}