-
Notifications
You must be signed in to change notification settings - Fork 0
/
superblock.c
205 lines (155 loc) · 7.14 KB
/
superblock.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
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
/*
* superblock.c
*
* Created on: Dec 18, 2014
* Author: Nir Dothan 028434009
*
* This module creates and handles everything that occurs at the superblock level or inside it i.e. the blocks
*/
#include "memory_allocator.h"
/*
* |+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
* | |
* | <--------------------------------------------SUPERBLOCK_SIZE + sizeof(sblk_metadata_t)--------------------------------> |
* | |
* | sblk_metadata_t |<-------------------------- SUPERBLOCK_SIZE -----------------------)--------------------------> |
* | | |
* | | |
* | |__________________________________________ |
* | | | | |
* | | block_header_t | sizeClass | |
* | | | | |
* | | | | |
* | |__________________________________________| |
* | |<--pointer sz-->|<--pointer sz--> <--pointer sz-->| |
* | | | |
* | | block size (in pointer units) | |
* | | = (2*header+sizeClass-1)/header | |
* | | | |
* |+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
*
*
*/
superblock_t* makeSuperblock(size_t sizeClassBytes) {
block_header_t *p, *pPrev = NULL;
size_t netSuperblockSize = SUPERBLOCK_SIZE;
size_t normSuperblockSize = netSuperblockSize / sizeof(block_header_t);
/* the offset between subsequent blocks in units of block_header_t */
size_t blockOffset = getBlockActualSizeInHeaders(sizeClassBytes);
/* the number of blocks that we'll generate in this superblock */
size_t numberOfBlocks = normSuperblockSize / blockOffset;
int i;
/* call system to allocate memory */
superblock_t *pSb = (superblock_t*) getCore(SUPERBLOCK_SIZE + sizeof(sblk_metadata_t));
pSb->_meta._sizeClassBytes = sizeClassBytes;
pSb->_meta._NoBlks = pSb->_meta._NoFreeBlks = numberOfBlocks;
pSb->_meta._pNxtSBlk = pSb->_meta._pPrvSblk = NULL;
/* initialize the working pointer to the address where allocated buffer begins */
p = (block_header_t*) pSb->_buff;
/* initialize the stack pointer to the first element in the list
* it will later be regarded as "top of the stack"
*/
pSb->_meta._pFreeBlkStack = p;
p->_pOwner = pSb;
p->size = sizeClassBytes;
/* create the initial free blocks stack inside the allocated memory buffer */
for (i = 0; i < numberOfBlocks - 1; i++) {
pPrev = p;
p += blockOffset;
pPrev->_pNextBlk = p;
p->_pOwner = pSb;
p->size = sizeClassBytes;
p->_pNextBlk = NULL;
}
pthread_mutex_init(&(pSb->_meta._sbLock),NULL);
return pSb;
}
/**
* pop a block from the top of the stack.
* caller must call the relocateSuperBlockBack on the owning heap to update the superblock's position
*/
block_header_t *popBlock(superblock_t *pSb) {
if (!pSb->_meta._NoFreeBlks)
return NULL; /* no free blocks */
/* get tail block */
block_header_t *pTail = pSb->_meta._pFreeBlkStack;
/* advance superblock tail */
pSb->_meta._pFreeBlkStack = pSb->_meta._pFreeBlkStack->_pNextBlk;
pSb->_meta._NoFreeBlks--;
/* disconnect from stack - but leave the owner for when the user wants to free it*/
pTail->_pNextBlk = NULL;
return pTail;
}
void *allocateFromSuperblock(superblock_t *pSb) {
block_header_t *block = popBlock(pSb);
relocateSuperBlockBack(
&pSb->_meta._pOwnerHeap->_sizeClasses[getSizeClassIndex(
pSb->_meta._sizeClassBytes)], pSb);
return (void*) (block + 1);
}
/**
* push a block to the top of the stack.
* caller must call the relocateSuperBlockAhead on the owning heap to update the superblock's position
*/
superblock_t *pushBlock(superblock_t *pSb, block_header_t *pBlk) {
if (pSb->_meta._NoFreeBlks == pSb->_meta._NoBlks)
return NULL; /* stack full */
/* new block's next is current tail */
pBlk->_pNextBlk = pSb->_meta._pFreeBlkStack;
/* stack tail points to new block */
pSb->_meta._pFreeBlkStack = pBlk;
pSb->_meta._NoFreeBlks++;
return pSb;
}
/* reruns the percentage (0-100) of used blocks out of total blocks */
unsigned short getFullness(superblock_t *pSb) {
double freeness = ((double) (pSb->_meta._NoFreeBlks))
/ ((double) (pSb->_meta._NoBlks));
double fullness = 1 - freeness;
return ((unsigned short) (fullness * 100));
}
/**
* utility function for printing a superblock
*/
void printSuperblock(superblock_t *pSb) {
unsigned int i;
block_header_t *p = pSb->_meta._pFreeBlkStack;
printf(" Superblock: [%p] blocks: [%u] free [%u] used bytes [%u]\n", pSb,
pSb->_meta._NoBlks, pSb->_meta._NoFreeBlks, getBytesUsed(pSb));
printf(" [%p]<----prev next---->[%p]\n", pSb->_meta._pPrvSblk,
pSb->_meta._pNxtSBlk);
printf(" ====================================\n");
for (i = 0; i < pSb->_meta._NoFreeBlks; i++, p = p->_pNextBlk) {
printf(" free block %u) [%p]\n", i, p);
}
}
/* returns the size in bytes of blocks used in superblock */
size_t getBytesUsed(const superblock_t *pSb) {
size_t usedBlocks = pSb->_meta._NoBlks - pSb->_meta._NoFreeBlks;
return usedBlocks * ( getBlockActualSizeInBytes(pSb->_meta._sizeClassBytes));
}
block_header_t *getBlockHeaderForPtr(void *ptr) {
block_header_t *pBlock = (block_header_t *)( ptr - sizeof(block_header_t));
return pBlock;
}
superblock_t *getSuperblockForPtr(void *ptr) {
block_header_t *pHeader = getBlockHeaderForPtr(ptr);
return pHeader->_pOwner;
}
/* for use with large allocations that do not use Hoard */
superblock_t* makeDummySuperblock(superblock_t *pSb, size_t sizeClassBytes) {
pSb->_meta._sizeClassBytes = sizeClassBytes;
return pSb;
}
void freeBlockFromSuperBlock(superblock_t *pSb, block_header_t *pBlock) {
if (!pushBlock(pSb, pBlock))
printf("Error freeing memory!\n");
}
/* the offset between subsequent blocks in units of block_header_t */
size_t getBlockActualSizeInHeaders(size_t sizeClassBytes){
return (sizeClassBytes + 2 * sizeof(block_header_t) - 1)
/ sizeof(block_header_t);
}
size_t getBlockActualSizeInBytes(size_t sizeClassBytes){
return getBlockActualSizeInHeaders(sizeClassBytes)*sizeof(block_header_t);
}