aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/other/memory.h
blob: 902edd2dfea585558e0bc6a3a0c65a16150fb627 (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
/*
 * Portable memory manipulation and management routines.
 * $Id$
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions of distribution and reuse see product license.
 */

#ifndef __OTHER_MEMORY_H__
#define __OTHER_MEMORY_H__

#ifndef __STD_TYPES_H__
#include <std/types.h>
#endif

#ifndef	MEMORY_H_MISSING
#include <memory.h>
#endif

#ifndef	__MEMALIGN
#define	__MEMALIGN	sizeof(ptr_t)
#endif

#ifndef	__OBJALIGN
#define	__OBJALIGN	__MEMALIGN
#endif

struct	_mempool;
struct	_mempage;
struct	_memfree;
struct	_memcell;

typedef struct _mempool
{
        int     mem_psize;
        int     mem_pcount;
        int     mem_pused;
        void    *(*mem_pfault)(struct _mempool *mem, int reqsize);
	struct	_memfree *mem_free;
        struct	_mempage *mem_reuse;
        struct	_mempage *mem_last;
}       MEMPOOL;

typedef struct _mempage
{
        struct	_mempage *page_next;
        int     page_used;
	uchar	page[ EMPTY ];
}       MEMPAGE;

typedef struct _memfree
{
        struct	_memfree *next;
        struct  _memcell *list;
        int     size;
}       _MEMFREE;

typedef	struct	_memcell
{
	void	*next;
}	_MEMCELL;

#define memfault(mem, fault)    mem->mem_pfault = fault;
#define memfirst(mem)           ((MEMPAGE *)((char *)(mem) - sizeof(MEMPAGE)))

#ifdef __cplusplus
extern "C" {
#endif

#ifdef	__NAMESPACE
#define	mempool		__NAMESPACE(mempool)
#define	memreuse 	__NAMESPACE(memreuse)
#define	memrelease	__NAMESPACE(memrelease)
#define	memreq		__NAMESPACE(memreq)
#define	memlreq		__NAMESPACE(memlreq)
#define	strreq		__NAMESPACE(strreq)
#define	strlreq		__NAMESPACE(strlreq)
#define	memdup		__NAMESPACE(memdup)
#define	memalloc	__NAMESPACE(memalloc)
#define	memfree		__NAMESPACE(memfree)
#endif

MEMPOOL *mempool(int psize, int pcount);
void 	memreuse(MEMPOOL *mem);
void    memrelease(MEMPOOL *mem);
void    *memreq(MEMPOOL *mem, size_t memsize);
void    *memlreq(MEMPOOL *mem, size_t memsize);
char    *strreq(MEMPOOL *mem, const char *str);
char    *strlreq(MEMPOOL *mem, const char *str);
void	*memdup(void *, size_t);

/* more advanced allocation schemes */

void    *memalloc(MEMPOOL *mem, int memsize);
void    memfree(MEMPOOL *mem, void *obj, int memsize);

#ifdef	__cplusplus
}
#endif

#endif