File: alloc.h

package info (click to toggle)
hybserv 1.9.2-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 2,848 kB
  • ctags: 1,854
  • sloc: ansic: 37,037; sh: 3,167; makefile: 337
file content (85 lines) | stat: -rw-r--r-- 2,407 bytes parent folder | download | duplicates (4)
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
/*
 * alloc.h
 * Hybserv2 Services by Hybserv2 team
 *
 * $Id: alloc.h 1350 2005-11-29 11:40:00Z kreator $
 */

#ifndef INCLUDED_alloc_h
#define INCLUDED_alloc_h

#include "stdinc.h"
#include "config.h"

#ifdef BLOCK_ALLOCATION
/* structure definition for a sub block in a preallocated heap */
typedef struct sBlock
{
	struct sBlock *next; /* next sub block in our heap */
	void *first;         /* pointer to first element in sub block */
	void *last;          /* pointer to last element in sub block */
	int FreeElements;    /* number of unused elements in sub block */

	/*
	 * This is the pointer to the last used element in the block,
	 * meaning every slot between 'LastUsedSlot' and 'last', will
	 * be unused. This is needed for BlockSubFree().
	 * Normally, (Heap->ElementsPerBlock - Block->FreeElements)
	 * would be the same thing, but if middle elements are deleted,
	 * Block->FreeElements would increase, while the last used
	 * element in the block would remain the same, so we need a
	 * separate pointer to keep track.
	 */
	void *LastUsedSlot;

	/*
	 * SlotHoles is an array of pointers to all of the
	 * "holes" in the sub block; when an element is deleted,
	 * its pointer is added to this array so the next time
	 * we add an element, we know where to fill in the hole
	 */
	void **SlotHoles;
	int LastSlotHole; /* number of used indices in SlotHoles[] */
}
SubBlock;

/* structure definition for a preallocated heap of memory */

typedef struct BlockHeap
{
	SubBlock *base;       /* first sub block in our heap */
	int ElementSize;      /* size of each element */
	int ElementsPerBlock; /* number of elements in each sub block */
	int NumSubBlocks;     /* number of sub blocks we have allocated */
	int FreeElements;     /* number of unused elements in all blocks */
}
Heap;

Heap *HeapCreate(size_t, int);
void *BlockSubAllocate(Heap *);
void BlockSubFree(Heap *, void *);
void InitHeaps(void);
#endif /* BLOCK_ALLOCATION */

void *MyMalloc(size_t);
void *MyRealloc(void *, size_t);
char *MyStrdup(const char *);
void OutOfMem(void);

/* MyFree - free an argument */
#define MyFree(ptr) \
{                   \
  if (ptr != NULL)  \
  {                 \
    free(ptr);      \
    ptr = NULL;     \
  }                 \
}

#ifdef BLOCK_ALLOCATION
extern Heap *ClientHeap;
extern Heap *ChannelHeap;
extern Heap *ServerHeap;
#endif /* BLOCK_ALLOCATION */

#endif /* INCLUDED_alloc_h */