-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmem.c
More file actions
81 lines (69 loc) · 1.94 KB
/
mem.c
File metadata and controls
81 lines (69 loc) · 1.94 KB
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
#ifdef __ELKS__
/* malloc/free wholesale replacement for 8086 toolchain */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MALLOC_ARENA_SIZE 42000U /* size of initial arena fmemalloc (max 65520)*/
#define MALLOC_ARENA_THRESH 1024U /* max size to allocate from arena-managed heap */
unsigned int malloc_arena_size = MALLOC_ARENA_SIZE;
unsigned int malloc_arena_thresh = MALLOC_ARENA_THRESH;
#define FP_SEG(fp) ((unsigned)((unsigned long)(void __far *)(fp) >> 16))
#define FP_OFF(fp) ((unsigned)(unsigned long)(void __far *)(fp))
static void __far *heap;
void *malloc(size_t size)
{
char *p;
if (heap == NULL) {
heap = fmemalloc(malloc_arena_size);
if (!heap) {
__dprintf("FATAL: Can't fmemalloc %u\n", malloc_arena_size);
system("meminfo > /dev/console");
exit(1);
}
_fmalloc_add_heap(heap, malloc_arena_size);
}
if (size <= malloc_arena_thresh)
{
p = _fmalloc(size);
if (p == NULL)
{
__dprintf("HEAP full: allocating from far memory %u\n", size);
p = fmemalloc(size);
}
}
else
p = fmemalloc(size);
return p;
}
void free(void *ptr)
{
if (ptr == NULL)
return;
if (FP_OFF(ptr) == 0) /* non-arena pointer */
fmemfree(ptr);
else
_ffree(ptr);
}
void *realloc(void *ptr, size_t size)
{
void *new;
size_t osize = size;
if (ptr == 0)
return malloc(size);
#if LATER
/* we can't yet get size from fmemalloc'd block */
osize = _fmalloc_usable_size(ptr);
__dprintf("old %u new %u\n", osize, size);
if (size < osize || osize == 0)
osize = size; /* copy less bytes in memcpy below */
#endif
new = malloc(size);
if (new == 0) {
__dprintf("realloc: Out of memory\n");
return 0;
}
memcpy(new, ptr, osize); /* FIXME copies too much but can't get real osize */
free(ptr);
return new;
}
#endif