| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "asterisk.h" |
| 17 |
#include "asterisk/lock.h" |
| 18 |
#include "asterisk/utils.h" |
| 19 |
#include <stdlib.h> |
| 20 |
#include "memheap.h" |
| 21 |
|
| 22 |
ASN1UINT g_defBlkSize = XM_K_MEMBLKSIZ; |
| 23 |
|
| 24 |
static OSMemLink* memHeapAddBlock (OSMemLink** ppMemLink, |
| 25 |
void* pMemBlk, int blockType); |
| 26 |
|
| 27 |
typedef void OSMemElemDescr; |
| 28 |
|
| 29 |
|
| 30 |
#define pElem_flags(pElem) (*((ASN1OCTET*)pElem)) |
| 31 |
#define pElem_nunits(pElem) (*((ASN1USINT*)(((ASN1OCTET*)pElem)+2))) |
| 32 |
#define pElem_prevOff(pElem) (*((ASN1USINT*)(((ASN1OCTET*)pElem)+4))) |
| 33 |
#define pElem_nextFreeOff(pElem) (*((ASN1USINT*)(((ASN1OCTET*)pElem)+6))) |
| 34 |
#define pElem_beginOff(pElem) (*((ASN1USINT*)(((ASN1OCTET*)pElem)+6))) |
| 35 |
#define sizeof_OSMemElemDescr 8 |
| 36 |
#define pElem_data(pElem) (((ASN1OCTET*)pElem)+sizeof_OSMemElemDescr) |
| 37 |
|
| 38 |
typedef struct MemBlk { |
| 39 |
OSMemLink* plink; |
| 40 |
ASN1USINT free_x; |
| 41 |
ASN1USINT freeMem; |
| 42 |
ASN1USINT nunits; |
| 43 |
ASN1USINT lastElemOff; |
| 44 |
ASN1USINT freeElemOff; |
| 45 |
ASN1USINT nsaved; |
| 46 |
|
| 47 |
ASN1USINT spare[2]; |
| 48 |
|
| 49 |
char data[8]; |
| 50 |
} OSMemBlk; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#define QOFFSETOF(pElem, pPrevElem) \ |
| 55 |
((ASN1USINT)(((unsigned)((char*)pElem - (char*)pPrevElem)) >> 3u)) |
| 56 |
|
| 57 |
#define OFFSETOF(pElem, pPrevElem) \ |
| 58 |
((ASN1UINT)((char*)pElem - (char*)pPrevElem)) |
| 59 |
|
| 60 |
#define ISFREE(pElem) (pElem_flags(pElem) & 1) |
| 61 |
#define SET_FREE(pElem) (pElem_flags(pElem) |= 1) |
| 62 |
#define CLEAR_FREE(pElem) (pElem_flags(pElem) &= (~1)) |
| 63 |
|
| 64 |
#define ISLAST(pElem) (pElem_flags(pElem) & 2) |
| 65 |
#define SET_LAST(pElem) (pElem_flags(pElem) |= 2) |
| 66 |
#define CLEAR_LAST(pElem) (pElem_flags(pElem) &= (~2)) |
| 67 |
|
| 68 |
#define ISSAVED(pElem) (pElem_flags(pElem) & 4) |
| 69 |
#define SET_SAVED(pMemBlk,pElem) do { \ |
| 70 |
(pElem_flags (pElem) |= 4); pMemBlk->nsaved++; } while (0) |
| 71 |
#define CLEAR_SAVED(pMemBlk,pElem) do { \ |
| 72 |
(pElem_flags (pElem) &= (~4)); pMemBlk->nsaved--; } while (0) |
| 73 |
|
| 74 |
#define ISFIRST(pElem) (int)(pElem_prevOff (pElem) == 0) |
| 75 |
|
| 76 |
#define GETPREV(pElem) \ |
| 77 |
((pElem_prevOff (pElem) == 0) ? 0 : \ |
| 78 |
((OSMemElemDescr*) (((char*)pElem) - (pElem_prevOff (pElem) * 8u)))) |
| 79 |
|
| 80 |
#define GETNEXT(pElem) \ |
| 81 |
((ISLAST (pElem)) ? 0 : \ |
| 82 |
((OSMemElemDescr*)(((char*)pElem) + ((pElem_nunits (pElem) + 1) * 8u)))) |
| 83 |
|
| 84 |
#define GET_NEXT_FREE(pElem) \ |
| 85 |
((pElem_nextFreeOff (pElem) == 0) ? 0 : \ |
| 86 |
((OSMemElemDescr*) (((char*)pElem) + (pElem_nextFreeOff (pElem) * 8u)))) |
| 87 |
|
| 88 |
#define GET_MEMBLK(pElem) \ |
| 89 |
((OSMemBlk*) (((char*)pElem) - (pElem_beginOff (pElem) * 8u) - \ |
| 90 |
sizeof (OSMemBlk) + sizeof ((OSMemBlk*)0)->data)) |
| 91 |
|
| 92 |
#define GET_LAST_ELEM(pMemBlk) \ |
| 93 |
((pMemBlk->lastElemOff == 0) ? 0 : \ |
| 94 |
(OSMemElemDescr*)&pMemBlk->data[(pMemBlk->lastElemOff - 1) * 8u]) |
| 95 |
|
| 96 |
#define SET_LAST_ELEM(pMemBlk, pElem) \ |
| 97 |
pMemBlk->lastElemOff = (ASN1USINT)((pElem == 0) ? 0 : \ |
| 98 |
(SET_LAST (pElem), (QOFFSETOF (pElem, pMemBlk->data) + 1))) |
| 99 |
|
| 100 |
#define GET_FREE_ELEM(pMemBlk) \ |
| 101 |
((pMemBlk->freeElemOff == 0) ? 0 : \ |
| 102 |
(OSMemElemDescr*)&pMemBlk->data[(pMemBlk->freeElemOff - 1) * 8u]) |
| 103 |
|
| 104 |
#define FORCE_SET_FREE_ELEM(pMemBlk, pElem) do { \ |
| 105 |
if (pElem == 0) { pMemBlk->freeElemOff = 0; break; } \ |
| 106 |
SET_FREE (pElem); \ |
| 107 |
pMemBlk->freeElemOff = (ASN1USINT)(QOFFSETOF (pElem, pMemBlk->data) + 1); \ |
| 108 |
} while (0) |
| 109 |
|
| 110 |
#define SET_FREE_ELEM(pMemBlk, pElem) setLastElem (pMemBlk, pElem) |
| 111 |
|
| 112 |
|
| 113 |
#define RTMEMDIAG1(msg) |
| 114 |
#define RTMEMDIAG2(msg,a) |
| 115 |
#define RTMEMDIAG3(msg,a,b) |
| 116 |
#define RTMEMDIAG4(msg,a,b,c) |
| 117 |
#define FILLFREEMEM(mem,size) |
| 118 |
#define FILLNEWMEM(mem,size) |
| 119 |
|
| 120 |
#define CHECKMEMELEM(memblk,elem) |
| 121 |
#define CHECKMEMBLOCK(memheap,memblk) |
| 122 |
#define CHECKMEMHEAP(memheap) |
| 123 |
#define TRACEMEMELEM(memblk, elem, name) |
| 124 |
#define TRACEFREE(memlink,name) |
| 125 |
|
| 126 |
|
| 127 |
static void setLastElem (OSMemBlk* pMemBlk, OSMemElemDescr* pElem) |
| 128 |
{ |
| 129 |
if (pElem == 0) { |
| 130 |
pMemBlk->freeElemOff = 0; |
| 131 |
return; |
| 132 |
} |
| 133 |
else if (ISLAST (pElem)) |
| 134 |
return; |
| 135 |
else if (pMemBlk->freeElemOff > QOFFSETOF (pElem, pMemBlk->data) + 1) { |
| 136 |
pElem_nextFreeOff (pElem) = QOFFSETOF (GET_FREE_ELEM (pMemBlk), pElem); |
| 137 |
FORCE_SET_FREE_ELEM (pMemBlk, pElem); |
| 138 |
} |
| 139 |
else if (pMemBlk->freeElemOff == 0) { |
| 140 |
pElem_nextFreeOff (pElem) = 0; |
| 141 |
FORCE_SET_FREE_ELEM (pMemBlk, pElem); |
| 142 |
} |
| 143 |
else { |
| 144 |
SET_FREE (pElem); |
| 145 |
pElem_nextFreeOff (pElem) = 0; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void* memHeapAlloc (void** ppvMemHeap, int nbytes) |
| 150 |
{ |
| 151 |
OSMemHeap* pMemHeap; |
| 152 |
OSMemLink* pMemLink, **ppMemLink; |
| 153 |
OSMemBlk* pMemBlk = 0; |
| 154 |
void* mem_p = NULL; |
| 155 |
unsigned remUnits; |
| 156 |
ASN1UINT nunits; |
| 157 |
|
| 158 |
if (ppvMemHeap == 0) |
| 159 |
return 0; |
| 160 |
|
| 161 |
if (*ppvMemHeap == 0) |
| 162 |
if (memHeapCreate (ppvMemHeap) != ASN_OK) |
| 163 |
return 0; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
nunits = (((unsigned)(nbytes + 7)) >> 3); |
| 168 |
|
| 169 |
pMemHeap = (OSMemHeap*) *ppvMemHeap; |
| 170 |
ast_mutex_lock(&pMemHeap->pLock); |
| 171 |
ppMemLink = &pMemHeap->phead; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if (nunits > (1<<16) - 2) { |
| 176 |
void *data; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
data = ast_malloc(nbytes); |
| 181 |
if (data == NULL) { |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
pMemLink = memHeapAddBlock (ppMemLink, data, RTMEMMALLOC | RTMEMRAW); |
| 185 |
if (pMemLink == 0) { |
| 186 |
ast_free(data); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
*(int*)(((char*)pMemLink) + sizeof (OSMemLink)) = nbytes; |
| 191 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 192 |
return data; |
| 193 |
} |
| 194 |
|
| 195 |
RTMEMDIAG2 ("memHeapAlloc: adjusted nbytes = %d\n", nbytes); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
for (pMemLink = *ppMemLink; pMemLink != 0; pMemLink = pMemLink->pnext) { |
| 200 |
if (pMemLink->blockType & RTMEMRAW) continue; |
| 201 |
else pMemBlk = (OSMemBlk*) pMemLink->pMemBlk; |
| 202 |
|
| 203 |
remUnits = pMemBlk->nunits - pMemBlk->free_x; |
| 204 |
|
| 205 |
if ((unsigned)(nunits + 1) <= remUnits) { |
| 206 |
OSMemElemDescr* pElem = (OSMemElemDescr*) |
| 207 |
&pMemBlk->data [((ASN1UINT)pMemBlk->free_x) * 8u]; |
| 208 |
OSMemElemDescr* pPrevElem; |
| 209 |
|
| 210 |
RTMEMDIAG1 ("memHeapAlloc: found existing slot..\n"); |
| 211 |
|
| 212 |
|
| 213 |
if (pMemBlk->free_x == 0) { |
| 214 |
pMemHeap->freeUnits -= pMemBlk->nunits; |
| 215 |
pMemHeap->freeBlocks--; |
| 216 |
} |
| 217 |
|
| 218 |
pElem_flags (pElem) = 0; |
| 219 |
if (pMemBlk->lastElemOff != 0) |
| 220 |
pElem_prevOff (pElem) = |
| 221 |
(ASN1USINT)(pMemBlk->free_x - pMemBlk->lastElemOff + 1); |
| 222 |
else |
| 223 |
pElem_prevOff (pElem) = 0; |
| 224 |
|
| 225 |
pPrevElem = GET_LAST_ELEM (pMemBlk); |
| 226 |
if (pPrevElem != 0) |
| 227 |
CLEAR_LAST (pPrevElem); |
| 228 |
|
| 229 |
pElem_nunits (pElem) = (ASN1USINT)nunits; |
| 230 |
pElem_beginOff (pElem) = QOFFSETOF (pElem, pMemBlk->data); |
| 231 |
pMemBlk->lastElemOff = (ASN1USINT)(pMemBlk->free_x + 1); |
| 232 |
|
| 233 |
mem_p = (void*) (pElem_data (pElem)); |
| 234 |
|
| 235 |
|
| 236 |
pMemBlk->free_x += nunits + 1; |
| 237 |
|
| 238 |
SET_LAST_ELEM (pMemBlk, pElem); |
| 239 |
|
| 240 |
FILLNEWMEM (mem_p, nunits * 8u); |
| 241 |
TRACEMEMELEM(pMemBlk, pElem, "Allocated"); |
| 242 |
CHECKMEMELEM (pMemBlk, pElem); |
| 243 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (0 == mem_p) { |
| 251 |
for (pMemLink = *ppMemLink; pMemLink != 0; pMemLink = pMemLink->pnext) { |
| 252 |
if (pMemLink->blockType & RTMEMRAW) continue; |
| 253 |
|
| 254 |
pMemBlk = (OSMemBlk*) pMemLink->pMemBlk; |
| 255 |
|
| 256 |
if (nunits <= (ASN1UINT)pMemBlk->freeMem) { |
| 257 |
OSMemElemDescr* pElem = GET_FREE_ELEM(pMemBlk), *pPrevFree = 0; |
| 258 |
|
| 259 |
RTMEMDIAG2 |
| 260 |
("memHeapAlloc: try to reuse empty elems in pMemBlk = 0x%x...\n", |
| 261 |
pMemBlk); |
| 262 |
|
| 263 |
while (pElem != 0) { |
| 264 |
if (ISFREE (pElem)) { |
| 265 |
if (nunits <= (ASN1UINT)pElem_nunits (pElem)) { |
| 266 |
RTMEMDIAG3 |
| 267 |
("memHeapAlloc: " |
| 268 |
"found an existing free element 0x%x, size %d\n", |
| 269 |
pElem, (pElem_nunits (pElem) * 8u)); |
| 270 |
|
| 271 |
if (pMemBlk->freeElemOff == |
| 272 |
QOFFSETOF (pElem, pMemBlk->data) + 1) |
| 273 |
{ |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
OSMemElemDescr* nextFree = GET_NEXT_FREE (pElem); |
| 278 |
FORCE_SET_FREE_ELEM (pMemBlk, nextFree); |
| 279 |
} |
| 280 |
else if (pPrevFree != 0) { |
| 281 |
OSMemElemDescr* pNextFree = GET_NEXT_FREE (pElem); |
| 282 |
if (pNextFree != 0) |
| 283 |
pElem_nextFreeOff (pPrevFree) = QOFFSETOF (pNextFree, |
| 284 |
pPrevFree); |
| 285 |
else |
| 286 |
pElem_nextFreeOff (pPrevFree) = 0; |
| 287 |
} |
| 288 |
|
| 289 |
CLEAR_FREE (pElem); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
pElem_beginOff (pElem) = QOFFSETOF (pElem, pMemBlk->data); |
| 294 |
|
| 295 |
pMemBlk->freeMem -= pElem_nunits (pElem); |
| 296 |
|
| 297 |
CHECKMEMELEM (pMemBlk, pElem); |
| 298 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 299 |
|
| 300 |
mem_p = memHeapRealloc |
| 301 |
(ppvMemHeap, pElem_data (pElem), nunits * 8u); |
| 302 |
if (mem_p != 0) { |
| 303 |
FILLNEWMEM (mem_p, nunits * 8u); |
| 304 |
TRACEMEMELEM(pMemBlk, pElem, "Allocated"); |
| 305 |
} |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
pPrevFree = pElem; |
| 310 |
pElem = GET_NEXT_FREE (pElem); |
| 311 |
} |
| 312 |
if (mem_p != 0) break; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (!mem_p) { |
| 320 |
ASN1UINT allocSize, dataUnits; |
| 321 |
ASN1OCTET* pmem; |
| 322 |
register ASN1UINT defBlkSize = pMemHeap->defBlkSize; |
| 323 |
|
| 324 |
RTMEMDIAG1 ("memHeapAlloc: alloc block..\n"); |
| 325 |
|
| 326 |
allocSize = (ASN1UINT) ((((ASN1UINT)nunits) * 8u) + |
| 327 |
sizeof (OSMemBlk) + sizeof_OSMemElemDescr); |
| 328 |
allocSize = (ASN1UINT) (allocSize < defBlkSize) ? defBlkSize : |
| 329 |
((allocSize + defBlkSize - 1) / defBlkSize * defBlkSize); |
| 330 |
dataUnits = (ASN1UINT)((allocSize - sizeof (OSMemBlk)) >> 3u); |
| 331 |
if (dataUnits >= (1u<<16)) { |
| 332 |
dataUnits = (ASN1UINT)((1u<<16) - 1); |
| 333 |
allocSize = (ASN1UINT) |
| 334 |
((((ASN1UINT)dataUnits) * 8u) + sizeof (OSMemBlk)); |
| 335 |
} |
| 336 |
|
| 337 |
pmem = (ASN1OCTET*) ast_malloc(allocSize + sizeof (OSMemLink)); |
| 338 |
if (0 != pmem) { |
| 339 |
OSMemElemDescr* pElem; |
| 340 |
|
| 341 |
pMemBlk = (OSMemBlk*) (pmem + sizeof (OSMemLink)); |
| 342 |
pElem = (OSMemElemDescr*)&pMemBlk->data[0]; |
| 343 |
|
| 344 |
mem_p = (void*) pElem_data (pElem); |
| 345 |
pElem_nunits (pElem) = (ASN1USINT)nunits; |
| 346 |
pElem_flags (pElem) = 0; |
| 347 |
pElem_prevOff (pElem) = 0; |
| 348 |
pElem_beginOff (pElem) = QOFFSETOF (pElem, pMemBlk->data); |
| 349 |
|
| 350 |
|
| 351 |
pMemBlk->free_x = (ASN1USINT)(nunits + 1); |
| 352 |
|
| 353 |
pMemBlk->freeMem = 0; |
| 354 |
pMemBlk->nunits = (ASN1USINT)dataUnits; |
| 355 |
SET_LAST_ELEM (pMemBlk, pElem); |
| 356 |
pMemBlk->freeElemOff = 0; |
| 357 |
pMemBlk->nsaved = 0; |
| 358 |
|
| 359 |
if (memHeapAddBlock (ppMemLink, pMemBlk, RTMEMSTD | RTMEMLINK) == 0) |
| 360 |
{ |
| 361 |
ast_free(pmem); |
| 362 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
pMemHeap->usedUnits += dataUnits; |
| 368 |
pMemHeap->usedBlocks++; |
| 369 |
|
| 370 |
FILLNEWMEM (mem_p, nunits * 8u); |
| 371 |
TRACEMEMELEM(pMemBlk, pElem, "Allocated"); |
| 372 |
CHECKMEMELEM (pMemBlk, pElem); |
| 373 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 374 |
} |
| 375 |
else { |
| 376 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 377 |
return NULL; |
| 378 |
} |
| 379 |
} |
| 380 |
RTMEMDIAG2 ("memHeapAlloc: pMemBlk = 0x%x\n", pMemBlk); |
| 381 |
RTMEMDIAG2 ("memHeapAlloc: pMemBlk->free_x = %d\n", pMemBlk->free_x); |
| 382 |
RTMEMDIAG2 ("memHeapAlloc: pMemBlk->size = %d\n", |
| 383 |
pMemBlk->nunits * 8u); |
| 384 |
RTMEMDIAG2 ("memHeapAlloc: mem_p = 0x%x\n", mem_p); |
| 385 |
RTMEMDIAG2 ("memHeapAlloc: sizeof (short) = %d\n", sizeof(short)); |
| 386 |
|
| 387 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 388 |
return (mem_p); |
| 389 |
} |
| 390 |
|
| 391 |
void* memHeapAllocZ (void** ppvMemHeap, int nbytes) |
| 392 |
{ |
| 393 |
void* ptr = memHeapAlloc (ppvMemHeap, nbytes); |
| 394 |
if (0 != ptr) memset (ptr, 0, nbytes); |
| 395 |
return ptr; |
| 396 |
} |
| 397 |
|
| 398 |
void memHeapFreePtr (void** ppvMemHeap, void* mem_p) |
| 399 |
{ |
| 400 |
OSMemHeap* pMemHeap; |
| 401 |
OSMemLink** ppMemLink; |
| 402 |
OSMemElemDescr* pElem; |
| 403 |
OSMemBlk* pMemBlk; |
| 404 |
OSMemLink* pMemLink, *pPrevMemLink = 0; |
| 405 |
|
| 406 |
RTMEMDIAG2 ("memHeapFreePtr: freeing mem_p = 0x%x\n", mem_p); |
| 407 |
|
| 408 |
if (mem_p == 0 || ppvMemHeap == 0 || *ppvMemHeap == 0) return; |
| 409 |
|
| 410 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 411 |
|
| 412 |
ast_mutex_lock(&pMemHeap->pLock); |
| 413 |
|
| 414 |
ppMemLink = &pMemHeap->phead; |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
for (pMemLink = *ppMemLink; pMemLink != 0; pMemLink = pMemLink->pnextRaw) { |
| 419 |
if ((pMemLink->blockType & RTMEMRAW) && |
| 420 |
pMemLink->pMemBlk == mem_p) |
| 421 |
{ |
| 422 |
if(pMemLink->pnext != 0) { |
| 423 |
pMemLink->pnext->pprev = pMemLink->pprev; |
| 424 |
} |
| 425 |
if(pMemLink->pprev != 0) { |
| 426 |
pMemLink->pprev->pnext = pMemLink->pnext; |
| 427 |
} |
| 428 |
else { |
| 429 |
*ppMemLink = pMemLink->pnext; |
| 430 |
} |
| 431 |
if (pPrevMemLink != 0) |
| 432 |
pPrevMemLink->pnextRaw = pMemLink->pnextRaw; |
| 433 |
else if (*ppMemLink != 0 && (*ppMemLink)->pnextRaw == 0 && |
| 434 |
*ppMemLink != pMemLink->pnextRaw) |
| 435 |
{ |
| 436 |
(*ppMemLink)->pnextRaw = pMemLink->pnextRaw; |
| 437 |
} |
| 438 |
if ((pMemLink->blockType & RTMEMLINK) && |
| 439 |
(pMemLink->blockType & RTMEMMALLOC)) |
| 440 |
{ |
| 441 |
ast_free(pMemLink); |
| 442 |
} |
| 443 |
else { |
| 444 |
if (pMemLink->blockType & RTMEMMALLOC) |
| 445 |
ast_free(pMemLink->pMemBlk); |
| 446 |
ast_free(pMemLink); |
| 447 |
} |
| 448 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 449 |
return; |
| 450 |
} |
| 451 |
pPrevMemLink = pMemLink; |
| 452 |
} |
| 453 |
|
| 454 |
pElem = (OSMemElemDescr*) (((char*)mem_p) - sizeof_OSMemElemDescr); |
| 455 |
pMemBlk = GET_MEMBLK (pElem); |
| 456 |
|
| 457 |
CHECKMEMELEM (pMemBlk, pElem); |
| 458 |
CHECKMEMBLOCK(pMemHeap, pMemBlk); |
| 459 |
|
| 460 |
if (ISFREE (pElem)) { |
| 461 |
RTMEMDIAG2 ("memHeapFreePtr: " |
| 462 |
"the element 0x%x is already freed!\n", pElem); |
| 463 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
if (ISSAVED (pElem)) { |
| 468 |
CLEAR_SAVED (pMemBlk, pElem); |
| 469 |
if (pMemBlk->nsaved == 0) |
| 470 |
pMemBlk->plink->blockType &= (~RTMEMSAVED); |
| 471 |
} |
| 472 |
|
| 473 |
TRACEMEMELEM(pMemBlk, pElem, "Freed"); |
| 474 |
CHECKMEMELEM (pMemBlk, pElem); |
| 475 |
CHECKMEMBLOCK(pMemHeap, pMemBlk); |
| 476 |
|
| 477 |
RTMEMDIAG2 ("memHeapFreePtr: pMemBlk = 0x%x\n", pMemBlk); |
| 478 |
RTMEMDIAG2 ("memHeapFreePtr: pMemBlk->size = %d\n", |
| 479 |
pMemBlk->nunits * 8u); |
| 480 |
|
| 481 |
if (ISLAST (pElem)) { |
| 482 |
OSMemElemDescr* pPrevElem = GETPREV (pElem); |
| 483 |
|
| 484 |
CHECKMEMELEM (pMemBlk, pPrevElem); |
| 485 |
|
| 486 |
pMemBlk->free_x -= (pElem_nunits (pElem) + 1); |
| 487 |
|
| 488 |
FILLFREEMEM (&pMemBlk->data [pMemBlk->free_x * 8u], |
| 489 |
(pElem_nunits (pElem) + 1) * 8u); |
| 490 |
|
| 491 |
if (pPrevElem != 0 && ISFREE (pPrevElem)) { |
| 492 |
OSMemElemDescr* pFreeElem; |
| 493 |
|
| 494 |
pMemBlk->free_x -= (pElem_nunits (pPrevElem) + 1); |
| 495 |
pMemBlk->freeMem -= pElem_nunits (pPrevElem); |
| 496 |
SET_LAST_ELEM (pMemBlk, GETPREV (pPrevElem)); |
| 497 |
|
| 498 |
|
| 499 |
if (pMemBlk->lastElemOff != 0) { |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
pFreeElem = GET_FREE_ELEM (pMemBlk); |
| 504 |
if (pFreeElem == pPrevElem) { |
| 505 |
pMemBlk->freeElemOff = 0; |
| 506 |
} |
| 507 |
else { |
| 508 |
OSMemElemDescr* pNextFree = 0; |
| 509 |
|
| 510 |
while (pFreeElem < pPrevElem) { |
| 511 |
pNextFree = pFreeElem; |
| 512 |
pFreeElem = GET_NEXT_FREE (pFreeElem); |
| 513 |
} |
| 514 |
pElem_nextFreeOff (pNextFree) = 0; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
else { |
| 519 |
SET_LAST_ELEM (pMemBlk, pPrevElem); |
| 520 |
} |
| 521 |
|
| 522 |
RTMEMDIAG2 ("memHeapFreePtr: pMemBlk->free_x = %d\n", |
| 523 |
pMemBlk->free_x); |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
if (pMemBlk->lastElemOff == 0) { |
| 528 |
|
| 529 |
if ((pMemHeap->flags & RT_MH_DONTKEEPFREE) || |
| 530 |
(pMemHeap->keepFreeUnits > 0 && |
| 531 |
pMemHeap->freeUnits + pMemBlk->nunits > pMemHeap->keepFreeUnits)) |
| 532 |
{ |
| 533 |
ASN1OCTET blockType = pMemBlk->plink->blockType; |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
pMemHeap->usedUnits -= pMemBlk->nunits; |
| 538 |
pMemHeap->usedBlocks --; |
| 539 |
|
| 540 |
if(pMemBlk->plink->pnext != 0) { |
| 541 |
pMemBlk->plink->pnext->pprev = pMemBlk->plink->pprev; |
| 542 |
} |
| 543 |
if(pMemBlk->plink->pprev != 0) { |
| 544 |
pMemBlk->plink->pprev->pnext = pMemBlk->plink->pnext; |
| 545 |
} |
| 546 |
else { |
| 547 |
if (pMemBlk->plink->pnext != 0 && |
| 548 |
!(pMemBlk->plink->pnext->blockType & RTMEMRAW)) |
| 549 |
{ |
| 550 |
pMemBlk->plink->pnext->pnextRaw = (*ppMemLink)->pnextRaw; |
| 551 |
} |
| 552 |
*ppMemLink = pMemBlk->plink->pnext; |
| 553 |
} |
| 554 |
FILLFREEMEM (pMemBlk->plink, sizeof (*pMemBlk->plink)); |
| 555 |
FILLFREEMEM (pMemBlk->data, (pMemBlk->nunits * 8u)); |
| 556 |
|
| 557 |
ast_free(pMemBlk->plink); |
| 558 |
|
| 559 |
if (!(blockType & RTMEMLINK)) { |
| 560 |
FILLFREEMEM (pMemBlk, sizeof (*pMemBlk)); |
| 561 |
ast_free(pMemBlk); |
| 562 |
} |
| 563 |
RTMEMDIAG2 ("memHeapFreePtr: pMemBlk = 0x%x was freed\n", |
| 564 |
pMemBlk); |
| 565 |
} |
| 566 |
else { |
| 567 |
|
| 568 |
pMemBlk->free_x = 0; |
| 569 |
pMemBlk->freeElemOff = 0; |
| 570 |
pMemBlk->lastElemOff = 0; |
| 571 |
pMemBlk->freeMem = 0; |
| 572 |
pMemBlk->nsaved = 0; |
| 573 |
pMemHeap->freeUnits += pMemBlk->nunits; |
| 574 |
pMemHeap->freeBlocks ++; |
| 575 |
} |
| 576 |
} |
| 577 |
else { |
| 578 |
SET_LAST (GET_LAST_ELEM (pMemBlk)); |
| 579 |
FILLFREEMEM (((char*) &pMemBlk->data[0]) + (pMemBlk->free_x * 8u), |
| 580 |
(pMemBlk->nunits - pMemBlk->free_x) * 8u); |
| 581 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 582 |
} |
| 583 |
} |
| 584 |
else { |
| 585 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 586 |
|
| 587 |
SET_FREE_ELEM(pMemBlk, pElem); |
| 588 |
|
| 589 |
pMemBlk->freeMem += pElem_nunits (pElem); |
| 590 |
RTMEMDIAG2 ("memHeapFreePtr: element 0x%x marked as free.\n", |
| 591 |
pElem); |
| 592 |
|
| 593 |
|
| 594 |
if (!ISFIRST (pElem)) { |
| 595 |
if (ISFREE (GETPREV (pElem))) { |
| 596 |
OSMemElemDescr* prevelem_p = GETPREV (pElem); |
| 597 |
|
| 598 |
|
| 599 |
pElem_nunits (prevelem_p) += pElem_nunits (pElem) + 1; |
| 600 |
|
| 601 |
pElem = prevelem_p; |
| 602 |
pMemBlk->freeMem ++; |
| 603 |
} |
| 604 |
else { |
| 605 |
|
| 606 |
|
| 607 |
OSMemElemDescr* prevelem_p = pElem; |
| 608 |
|
| 609 |
do { |
| 610 |
prevelem_p = GETPREV (prevelem_p); |
| 611 |
} |
| 612 |
while (prevelem_p && !ISFREE (prevelem_p)); |
| 613 |
|
| 614 |
if (prevelem_p != 0) { |
| 615 |
OSMemElemDescr* pNextFree = GET_NEXT_FREE (prevelem_p); |
| 616 |
if (pNextFree != 0) |
| 617 |
pElem_nextFreeOff (pElem) = QOFFSETOF (pNextFree, pElem); |
| 618 |
else |
| 619 |
pElem_nextFreeOff (pElem) = 0; |
| 620 |
pElem_nextFreeOff (prevelem_p) = QOFFSETOF (pElem, prevelem_p); |
| 621 |
|
| 622 |
CHECKMEMELEM (pMemBlk, prevelem_p); |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
if (pElem && !ISLAST (pElem) && ISFREE (GETNEXT (pElem))) { |
| 627 |
OSMemElemDescr* nextelem_p = GETNEXT (pElem); |
| 628 |
|
| 629 |
|
| 630 |
pElem_nunits (pElem) += pElem_nunits (nextelem_p) + 1; |
| 631 |
|
| 632 |
if (pElem_nextFreeOff (nextelem_p) == 0) |
| 633 |
pElem_nextFreeOff (pElem) = 0; |
| 634 |
else |
| 635 |
pElem_nextFreeOff (pElem) = |
| 636 |
QOFFSETOF (GET_NEXT_FREE (nextelem_p), pElem); |
| 637 |
pMemBlk->freeMem ++; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
if (pElem && !ISLAST (pElem)) { |
| 642 |
OSMemElemDescr* nextelem_p = GETNEXT (pElem); |
| 643 |
pElem_prevOff (nextelem_p) = QOFFSETOF (nextelem_p, pElem); |
| 644 |
} |
| 645 |
|
| 646 |
CHECKMEMELEM (pMemBlk, pElem); |
| 647 |
FILLFREEMEM (pElem_data (pElem), (pElem_nunits (pElem) * 8u)); |
| 648 |
CHECKMEMELEM (pMemBlk, pElem); |
| 649 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 650 |
} |
| 651 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 652 |
} |
| 653 |
|
| 654 |
static void initNewFreeElement (OSMemBlk* pMemBlk, |
| 655 |
OSMemElemDescr* pNewElem, OSMemElemDescr* pElem) |
| 656 |
{ |
| 657 |
OSMemElemDescr *pNextElem, *pPrevElem = 0; |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
pElem_flags (pNewElem) = 0; |
| 662 |
SET_FREE (pNewElem); |
| 663 |
|
| 664 |
pElem_prevOff (pNewElem) = QOFFSETOF (pNewElem, pElem); |
| 665 |
|
| 666 |
if (pMemBlk->freeElemOff != 0 && |
| 667 |
pMemBlk->freeElemOff < QOFFSETOF (pElem, pMemBlk->data) + 1) |
| 668 |
{ |
| 669 |
|
| 670 |
|
| 671 |
pPrevElem = pElem; |
| 672 |
|
| 673 |
do { |
| 674 |
pPrevElem = GETPREV (pPrevElem); |
| 675 |
} |
| 676 |
while (pPrevElem && !ISFREE (pPrevElem)); |
| 677 |
} |
| 678 |
if (pPrevElem != 0) { |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
pElem_nextFreeOff (pPrevElem) = QOFFSETOF (pNewElem, pPrevElem); |
| 683 |
} |
| 684 |
else { |
| 685 |
FORCE_SET_FREE_ELEM (pMemBlk, pNewElem); |
| 686 |
} |
| 687 |
|
| 688 |
pNextElem = GETNEXT (pNewElem); |
| 689 |
if (pNextElem && ISFREE (pNextElem)) { |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
pElem_nunits (pNewElem) += pElem_nunits (pNextElem) + 1; |
| 694 |
if (pElem_nextFreeOff (pNextElem) != 0) |
| 695 |
pElem_nextFreeOff (pNewElem) = QOFFSETOF (GET_NEXT_FREE (pNextElem), |
| 696 |
pNewElem); |
| 697 |
else |
| 698 |
pElem_nextFreeOff (pNewElem) = 0; |
| 699 |
pMemBlk->freeMem++; |
| 700 |
pNextElem = GETNEXT (pNewElem); |
| 701 |
} |
| 702 |
pElem_prevOff (pNextElem) = QOFFSETOF (pNextElem, pNewElem); |
| 703 |
|
| 704 |
if (pMemBlk->freeElemOff != 0) { |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
pNextElem = GETNEXT (pNewElem); |
| 709 |
while (pNextElem != 0 && !ISFREE (pNextElem)) |
| 710 |
pNextElem = GETNEXT (pNextElem); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
if (pNextElem != 0) |
| 715 |
pElem_nextFreeOff (pNewElem) = QOFFSETOF (pNextElem, pNewElem); |
| 716 |
else |
| 717 |
pElem_nextFreeOff (pNewElem) = 0; |
| 718 |
} |
| 719 |
else |
| 720 |
pElem_nextFreeOff (pNewElem) = 0; |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
void* memHeapRealloc (void** ppvMemHeap, void* mem_p, int nbytes_) |
| 725 |
{ |
| 726 |
OSMemHeap* pMemHeap; |
| 727 |
OSMemLink** ppMemLink; |
| 728 |
OSMemBlk* pMemBlk; |
| 729 |
OSMemElemDescr* pElem; |
| 730 |
OSMemLink* pMemLink; |
| 731 |
void *newMem_p; |
| 732 |
unsigned nbytes, nunits; |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0) return 0; |
| 737 |
|
| 738 |
if (mem_p == 0) { |
| 739 |
return memHeapAlloc (ppvMemHeap, nbytes_); |
| 740 |
} |
| 741 |
|
| 742 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 743 |
ppMemLink = &pMemHeap->phead; |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
for (pMemLink = *ppMemLink; pMemLink != 0; pMemLink = pMemLink->pnextRaw) { |
| 748 |
if ((pMemLink->blockType & RTMEMRAW) && |
| 749 |
pMemLink->pMemBlk == mem_p) |
| 750 |
{ |
| 751 |
if (pMemLink->blockType & RTMEMMALLOC) { |
| 752 |
void *newMemBlk = ast_realloc(pMemLink->pMemBlk, nbytes_); |
| 753 |
if (newMemBlk == 0) |
| 754 |
return 0; |
| 755 |
pMemLink->pMemBlk = newMemBlk; |
| 756 |
} |
| 757 |
else { |
| 758 |
return 0; |
| 759 |
} |
| 760 |
*(int*)(((char*)pMemLink) + sizeof (OSMemLink)) = nbytes_; |
| 761 |
return pMemLink->pMemBlk; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
nbytes = ((unsigned)(nbytes_ + 7)) & (~7); |
| 768 |
nunits = nbytes >> 3; |
| 769 |
|
| 770 |
pElem = (OSMemElemDescr*) (((char*)mem_p) - sizeof_OSMemElemDescr); |
| 771 |
|
| 772 |
RTMEMDIAG3 ("memHeapRealloc: mem_p = 0x%x, old size = %d,", mem_p, |
| 773 |
pElem_nunits (pElem) * 8u); |
| 774 |
RTMEMDIAG2 (" new nbytes = %d\n", nbytes); |
| 775 |
|
| 776 |
if ((unsigned)pElem_nunits (pElem) == nunits) |
| 777 |
return mem_p; |
| 778 |
|
| 779 |
pMemBlk = GET_MEMBLK (pElem); |
| 780 |
|
| 781 |
CHECKMEMELEM (pMemBlk, pElem); |
| 782 |
CHECKMEMBLOCK(pMemHeap, pMemBlk); |
| 783 |
|
| 784 |
if ((unsigned)pElem_nunits (pElem) < nunits) { |
| 785 |
|
| 786 |
if (nunits - pElem_nunits (pElem) <= (unsigned)pMemBlk->nunits) { |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
if (ISLAST (pElem)) { |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if ((int)(nunits - pElem_nunits (pElem)) <= |
| 795 |
(int)(pMemBlk->nunits - pMemBlk->free_x)) |
| 796 |
{ |
| 797 |
pMemBlk->free_x += nunits - pElem_nunits (pElem); |
| 798 |
pElem_nunits (pElem) = (ASN1USINT)nunits; |
| 799 |
|
| 800 |
RTMEMDIAG1 ("memHeapRealloc: " |
| 801 |
"memory element is expanded.\n"); |
| 802 |
|
| 803 |
FILLNEWMEM (&pMemBlk->data [(pMemBlk->free_x - |
| 804 |
(nunits - pElem_nunits (pElem))) * 8u], |
| 805 |
(nunits - pElem_nunits (pElem)) * 8u); |
| 806 |
|
| 807 |
TRACEMEMELEM (pMemBlk, pElem, "Reallocated"); |
| 808 |
CHECKMEMELEM (pMemBlk, pElem); |
| 809 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 810 |
|
| 811 |
return (mem_p); |
| 812 |
} |
| 813 |
} |
| 814 |
else { |
| 815 |
OSMemElemDescr* pNextElem, *pFreeElem; |
| 816 |
unsigned sumSize = pElem_nunits (pElem), freeMem = 0; |
| 817 |
|
| 818 |
RTMEMDIAG1 ("memHeapRealloc: look for free element after " |
| 819 |
"current block.\n"); |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
pNextElem = GETNEXT (pElem); |
| 824 |
if (pNextElem && ISFREE (pNextElem)) { |
| 825 |
|
| 826 |
sumSize += pElem_nunits (pNextElem) + 1; |
| 827 |
freeMem++; |
| 828 |
} |
| 829 |
|
| 830 |
if (sumSize >= nunits) { |
| 831 |
|
| 832 |
RTMEMDIAG1 ("memHeapRealloc: reuse free element.\n"); |
| 833 |
|
| 834 |
if (ISFREE (pNextElem)) { |
| 835 |
pFreeElem = GET_FREE_ELEM (pMemBlk); |
| 836 |
if (pFreeElem == pNextElem) { |
| 837 |
FORCE_SET_FREE_ELEM (pMemBlk, GET_NEXT_FREE (pNextElem)); |
| 838 |
} |
| 839 |
else if (pFreeElem < pElem) { |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
for (; pFreeElem != 0 && pFreeElem < pNextElem;) { |
| 844 |
OSMemElemDescr* pNextFreeElem = |
| 845 |
GET_NEXT_FREE (pFreeElem); |
| 846 |
if (pNextFreeElem == pNextElem) { |
| 847 |
if (pElem_nextFreeOff (pNextElem) != 0) |
| 848 |
pElem_nextFreeOff (pFreeElem) = QOFFSETOF |
| 849 |
(GET_NEXT_FREE (pNextElem), pFreeElem); |
| 850 |
else |
| 851 |
pElem_nextFreeOff (pFreeElem) = 0; |
| 852 |
CHECKMEMELEM (pMemBlk, pFreeElem); |
| 853 |
break; |
| 854 |
} |
| 855 |
pFreeElem = pNextFreeElem; |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
pMemBlk->freeMem += freeMem; |
| 863 |
|
| 864 |
if (sumSize - nunits > 1) { |
| 865 |
OSMemElemDescr* pNewElem; |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
pNewElem = (OSMemElemDescr*) |
| 870 |
(pElem_data (pElem) + nbytes); |
| 871 |
pElem_nunits (pNewElem) = (ASN1USINT)(sumSize - nunits - 1); |
| 872 |
|
| 873 |
initNewFreeElement (pMemBlk, pNewElem, pElem); |
| 874 |
|
| 875 |
pMemBlk->freeMem--; |
| 876 |
pMemBlk->freeMem -= (nunits - pElem_nunits (pElem)); |
| 877 |
pElem_nunits (pElem) = (ASN1USINT)nunits; |
| 878 |
} |
| 879 |
else { |
| 880 |
pMemBlk->freeMem -= (sumSize - pElem_nunits (pElem)); |
| 881 |
pElem_nunits (pElem) = (ASN1USINT)sumSize; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
pNextElem = GETNEXT (pElem); |
| 886 |
if (pNextElem != 0) |
| 887 |
pElem_prevOff (pNextElem) = QOFFSETOF (pNextElem, pElem); |
| 888 |
} |
| 889 |
|
| 890 |
TRACEMEMELEM (pMemBlk, pElem, "Reallocated"); |
| 891 |
CHECKMEMELEM (pMemBlk, pElem); |
| 892 |
CHECKMEMELEM (pMemBlk, (!ISLAST (pElem)) ? GETNEXT (pElem) : 0); |
| 893 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 894 |
return (mem_p); |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
RTMEMDIAG1 ("memHeapRealloc: allocate new memory...\n"); |
| 902 |
|
| 903 |
CHECKMEMHEAP (pMemHeap); |
| 904 |
|
| 905 |
newMem_p = memHeapAlloc (ppvMemHeap, nbytes); |
| 906 |
|
| 907 |
if (newMem_p == 0) |
| 908 |
return 0; |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
if (ISSAVED (pElem)) |
| 914 |
memHeapMarkSaved (ppvMemHeap, newMem_p, TRUE); |
| 915 |
|
| 916 |
CHECKMEMHEAP (pMemHeap); |
| 917 |
|
| 918 |
memcpy (newMem_p, mem_p, (((ASN1UINT)pElem_nunits (pElem)) * 8u)); |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
RTMEMDIAG1 ("memHeapRealloc: free old pointer...\n"); |
| 923 |
|
| 924 |
memHeapFreePtr (ppvMemHeap, mem_p); |
| 925 |
|
| 926 |
CHECKMEMHEAP (pMemHeap); |
| 927 |
|
| 928 |
return (newMem_p); |
| 929 |
} |
| 930 |
else { |
| 931 |
RTMEMDIAG1 ("memHeapRealloc: shrinking ...\n"); |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if (nbytes == 0) { |
| 936 |
RTMEMDIAG1 ("memHeapRealloc: free pointer...\n"); |
| 937 |
memHeapFreePtr (ppvMemHeap, mem_p); |
| 938 |
return (NULL); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
if (pElem_nunits (pElem) - nunits > 1) { |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
if (ISLAST (pElem)) { |
| 950 |
pMemBlk->free_x -= (pElem_nunits (pElem) - nunits); |
| 951 |
|
| 952 |
FILLFREEMEM (&pMemBlk->data [pMemBlk->free_x * 8u], |
| 953 |
(pElem_nunits (pElem) - nunits) * 8u); |
| 954 |
} |
| 955 |
else { |
| 956 |
OSMemElemDescr* pNewElem; |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
pNewElem = (OSMemElemDescr*) (pElem_data (pElem) + nbytes); |
| 961 |
|
| 962 |
|
| 963 |
pElem_nunits (pNewElem) = (ASN1USINT)(pElem_nunits (pElem) - nunits - 1); |
| 964 |
|
| 965 |
initNewFreeElement (pMemBlk, pNewElem, pElem); |
| 966 |
|
| 967 |
pMemBlk->freeMem += (pElem_nunits (pElem) - nunits) - 1; |
| 968 |
} |
| 969 |
pElem_nunits (pElem) = (ASN1USINT)nunits; |
| 970 |
|
| 971 |
TRACEMEMELEM (pMemBlk, pElem, "Reallocated"); |
| 972 |
CHECKMEMELEM (pMemBlk, pElem); |
| 973 |
CHECKMEMELEM (pMemBlk, (!ISLAST (pElem)) ? GETNEXT (pElem) : pElem); |
| 974 |
CHECKMEMBLOCK (pMemHeap, pMemBlk); |
| 975 |
} |
| 976 |
return (mem_p); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
void memHeapFreeAll (void** ppvMemHeap) |
| 982 |
{ |
| 983 |
OSMemHeap* pMemHeap; |
| 984 |
OSMemLink* pMemLink; |
| 985 |
OSMemLink* pMemLink2; |
| 986 |
|
| 987 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0) return; |
| 988 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 989 |
|
| 990 |
ast_mutex_lock(&pMemHeap->pLock); |
| 991 |
|
| 992 |
pMemLink = pMemHeap->phead; |
| 993 |
RTMEMDIAG2 ("memHeapFreeAll: pMemHeap = 0x%x\n", pMemHeap); |
| 994 |
|
| 995 |
TRACEFREE (pMemHeap, "memHeapFreeAll\n\n"); |
| 996 |
CHECKMEMHEAP (pMemHeap); |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
while (pMemLink) { |
| 1001 |
pMemLink2 = pMemLink; |
| 1002 |
pMemLink = pMemLink2->pnext; |
| 1003 |
|
| 1004 |
RTMEMDIAG3 ("memHeapFreeAll: pMemLink2 = 0x%x, pMemLink = 0x%x\n", |
| 1005 |
pMemLink2, pMemLink); |
| 1006 |
|
| 1007 |
#ifdef _MEMDEBUG |
| 1008 |
if (pMemLink2->blockType & RTMEMSTD) { |
| 1009 |
OSMemBlk* pMemBlk = (OSMemBlk*) pMemLink2->pMemBlk; |
| 1010 |
FILLFREEMEM (pMemBlk->data, (pMemBlk->nunits * 8u)); |
| 1011 |
FILLFREEMEM (pMemBlk, sizeof (*pMemBlk)); |
| 1012 |
} |
| 1013 |
#endif |
| 1014 |
if (!(pMemLink2->blockType & RTMEMSAVED)) { |
| 1015 |
OSMemBlk* pMemBlk = (OSMemBlk*) pMemLink2->pMemBlk; |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
if(pMemLink2->pnext != 0) { |
| 1020 |
pMemLink2->pnext->pprev = pMemLink2->pprev; |
| 1021 |
} |
| 1022 |
if(pMemLink2->pprev != 0) { |
| 1023 |
pMemLink2->pprev->pnext = pMemLink2->pnext; |
| 1024 |
} |
| 1025 |
else { |
| 1026 |
pMemHeap->phead = pMemLink2->pnext; |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
pMemHeap->usedUnits -= pMemBlk->nunits; |
| 1032 |
|
| 1033 |
if (pMemBlk->free_x == 0) |
| 1034 |
pMemHeap->freeBlocks --; |
| 1035 |
else |
| 1036 |
pMemHeap->usedBlocks --; |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
if (((pMemLink2->blockType & RTMEMSTD) || |
| 1041 |
(pMemLink2->blockType & RTMEMMALLOC)) && |
| 1042 |
!(pMemLink2->blockType & RTMEMLINK)) |
| 1043 |
ast_free(pMemLink2->pMemBlk); |
| 1044 |
ast_free(pMemLink2); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
void memHeapAddRef (void** ppvMemHeap) |
| 1052 |
{ |
| 1053 |
OSMemHeap* pMemHeap; |
| 1054 |
|
| 1055 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0) return; |
| 1056 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 1057 |
ast_mutex_lock(&pMemHeap->pLock); |
| 1058 |
pMemHeap->refCnt++; |
| 1059 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
void memHeapRelease (void** ppvMemHeap) |
| 1064 |
{ |
| 1065 |
OSMemHeap** ppMemHeap = (OSMemHeap**)ppvMemHeap; |
| 1066 |
OSMemHeap* pMemHeap; |
| 1067 |
|
| 1068 |
if (ppMemHeap != 0 && *ppMemHeap != 0 && --(*ppMemHeap)->refCnt == 0) { |
| 1069 |
OSMemLink* pMemLink, *pMemLink2; |
| 1070 |
|
| 1071 |
memHeapFreeAll (ppvMemHeap); |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
pMemLink = (*ppMemHeap)->phead; |
| 1076 |
while (pMemLink) { |
| 1077 |
pMemLink2 = pMemLink; |
| 1078 |
pMemLink = pMemLink2->pnext; |
| 1079 |
|
| 1080 |
ast_free(pMemLink2); |
| 1081 |
} |
| 1082 |
|
| 1083 |
if ((*ppMemHeap)->flags & RT_MH_FREEHEAPDESC) { |
| 1084 |
pMemHeap = *ppMemHeap; |
| 1085 |
ast_mutex_destroy(&pMemHeap->pLock); |
| 1086 |
ast_free(*ppMemHeap); |
| 1087 |
} |
| 1088 |
*ppMemHeap = 0; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
void* memHeapMarkSaved (void** ppvMemHeap, const void* mem_p, |
| 1098 |
ASN1BOOL saved) |
| 1099 |
{ |
| 1100 |
OSMemHeap* pMemHeap; |
| 1101 |
OSMemLink* pMemLink; |
| 1102 |
ASN1UINT nsaved = 1; |
| 1103 |
|
| 1104 |
RTMEMDIAG2 ("memHeapMarkSaved: for mem_p = 0x%x\n", mem_p); |
| 1105 |
|
| 1106 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0 || mem_p == 0) |
| 1107 |
return 0; |
| 1108 |
|
| 1109 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 1110 |
|
| 1111 |
ast_mutex_lock(&pMemHeap->pLock); |
| 1112 |
|
| 1113 |
pMemLink = pMemHeap->phead; |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
for (; pMemLink != 0; pMemLink = pMemLink->pnextRaw) { |
| 1118 |
if ((pMemLink->blockType & RTMEMRAW) && |
| 1119 |
pMemLink->pMemBlk == mem_p) |
| 1120 |
{ |
| 1121 |
break; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
if (pMemLink == 0) { |
| 1125 |
OSMemElemDescr* pElem; |
| 1126 |
OSMemBlk* pMemBlk; |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
pElem = (OSMemElemDescr*) (((char*)mem_p) - sizeof_OSMemElemDescr); |
| 1131 |
|
| 1132 |
if (ISFREE (pElem)) { |
| 1133 |
RTMEMDIAG2 ("memHeapMarkSaved: the element 0x%x is " |
| 1134 |
"already free!\n", pElem); |
| 1135 |
|
| 1136 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
if ((ISSAVED (pElem) && !saved) || (!ISSAVED (pElem) && saved)) { |
| 1141 |
|
| 1142 |
pMemBlk = GET_MEMBLK (pElem); |
| 1143 |
|
| 1144 |
CHECKMEMELEM (pMemBlk, pElem); |
| 1145 |
CHECKMEMBLOCK(pMemHeap, pMemBlk); |
| 1146 |
|
| 1147 |
pMemLink = pMemBlk->plink; |
| 1148 |
|
| 1149 |
if (saved) |
| 1150 |
SET_SAVED (pMemBlk, pElem); |
| 1151 |
else |
| 1152 |
CLEAR_SAVED (pMemBlk, pElem); |
| 1153 |
nsaved = pMemBlk->nsaved; |
| 1154 |
} |
| 1155 |
else { |
| 1156 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1157 |
} |
| 1158 |
return 0; |
| 1159 |
} |
| 1160 |
if (saved && nsaved > 0) |
| 1161 |
pMemLink->blockType |= RTMEMSAVED; |
| 1162 |
else if (nsaved == 0) |
| 1163 |
pMemLink->blockType &= (~RTMEMSAVED); |
| 1164 |
|
| 1165 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1166 |
return pMemLink->pMemBlk; |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
void memHeapReset (void** ppvMemHeap) |
| 1173 |
{ |
| 1174 |
OSMemHeap* pMemHeap; |
| 1175 |
OSMemLink *pMemLink; |
| 1176 |
|
| 1177 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0) return; |
| 1178 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 1179 |
|
| 1180 |
ast_mutex_lock(&pMemHeap->pLock); |
| 1181 |
|
| 1182 |
pMemLink = pMemHeap->phead; |
| 1183 |
TRACEFREE (pMemHeap, "memHeapReset\n\n"); |
| 1184 |
while (pMemLink) { |
| 1185 |
if (!(pMemLink->blockType & RTMEMSAVED)) { |
| 1186 |
if (pMemLink->blockType & RTMEMSTD) { |
| 1187 |
OSMemBlk* pMemBlk = (OSMemBlk*) pMemLink->pMemBlk; |
| 1188 |
if (pMemBlk->free_x != 0) { |
| 1189 |
pMemHeap->freeUnits += pMemBlk->nunits; |
| 1190 |
pMemHeap->freeBlocks ++; |
| 1191 |
} |
| 1192 |
pMemBlk->free_x = 0; |
| 1193 |
pMemBlk->freeElemOff = 0; |
| 1194 |
pMemBlk->lastElemOff = 0; |
| 1195 |
pMemBlk->freeMem = 0; |
| 1196 |
FILLFREEMEM (pMemBlk->data, pMemBlk->nunits * 8u); |
| 1197 |
} |
| 1198 |
else if (pMemLink->blockType & RTMEMRAW) { |
| 1199 |
|
| 1200 |
memHeapFreePtr (ppvMemHeap, pMemLink->pMemBlk); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
pMemLink = pMemLink->pnext; |
| 1204 |
} |
| 1205 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
static OSMemLink* memHeapAddBlock (OSMemLink** ppMemLink, |
| 1211 |
void* pMemBlk, int blockType) |
| 1212 |
{ |
| 1213 |
OSMemLink* pMemLink; |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
if (blockType & RTMEMLINK) |
| 1220 |
pMemLink = (OSMemLink*) (((ASN1OCTET*)pMemBlk) - sizeof (OSMemLink)); |
| 1221 |
else { |
| 1222 |
pMemLink = ast_malloc(sizeof(OSMemLink) + sizeof(int)); |
| 1223 |
if (pMemLink == 0) return 0; |
| 1224 |
|
| 1225 |
|
| 1226 |
*(int*)(((char*)pMemLink) + sizeof (OSMemLink)) = (int)-1; |
| 1227 |
} |
| 1228 |
if (pMemLink == NULL) |
| 1229 |
return NULL; |
| 1230 |
pMemLink->blockType = (ASN1OCTET)blockType; |
| 1231 |
pMemLink->pMemBlk = pMemBlk; |
| 1232 |
pMemLink->pprev = 0; |
| 1233 |
pMemLink->pnext = *ppMemLink; |
| 1234 |
|
| 1235 |
if (*ppMemLink != 0) { |
| 1236 |
if ((*ppMemLink)->blockType & RTMEMRAW) |
| 1237 |
pMemLink->pnextRaw = *ppMemLink; |
| 1238 |
else { |
| 1239 |
pMemLink->pnextRaw = (*ppMemLink)->pnextRaw; |
| 1240 |
(*ppMemLink)->pnextRaw = 0; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
else { |
| 1244 |
pMemLink->pnextRaw = 0; |
| 1245 |
} |
| 1246 |
|
| 1247 |
*ppMemLink = pMemLink; |
| 1248 |
if (pMemLink->pnext != 0) |
| 1249 |
pMemLink->pnext->pprev = pMemLink; |
| 1250 |
((OSMemBlk*)pMemBlk)->plink = pMemLink; |
| 1251 |
|
| 1252 |
RTMEMDIAG2 ("memHeapAddBlock: pMemLink = 0x%x\n", pMemLink); |
| 1253 |
RTMEMDIAG2 ("memHeapAddBlock: pMemLink->pnext = 0x%x\n", |
| 1254 |
pMemLink->pnext); |
| 1255 |
RTMEMDIAG2 ("memHeapAddBlock: pMemLink->pprev = 0x%x\n", |
| 1256 |
pMemLink->pprev); |
| 1257 |
|
| 1258 |
return pMemLink; |
| 1259 |
} |
| 1260 |
|
| 1261 |
int memHeapCheckPtr (void** ppvMemHeap, void* mem_p) |
| 1262 |
{ |
| 1263 |
OSMemHeap* pMemHeap; |
| 1264 |
OSMemLink* pMemLink; |
| 1265 |
|
| 1266 |
RTMEMDIAG2 ("memHeapCheckPtr: for mem_p = 0x%x\n", mem_p); |
| 1267 |
|
| 1268 |
if (ppvMemHeap == 0 || *ppvMemHeap == 0 || mem_p == 0) |
| 1269 |
return 0; |
| 1270 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 1271 |
|
| 1272 |
ast_mutex_lock(&pMemHeap->pLock); |
| 1273 |
|
| 1274 |
pMemLink = pMemHeap->phead; |
| 1275 |
|
| 1276 |
for (; pMemLink != 0; pMemLink = pMemLink->pnext) { |
| 1277 |
if (pMemLink->blockType & RTMEMRAW) { |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
if (pMemLink->pMemBlk == mem_p) { |
| 1282 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1283 |
return 1; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
else { |
| 1287 |
OSMemBlk* pMemBlk = (OSMemBlk*)pMemLink->pMemBlk; |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
if (mem_p > pMemLink->pMemBlk && |
| 1292 |
mem_p < (void*)(((ASN1OCTET*)pMemLink->pMemBlk) + pMemBlk->nunits * 8u)) |
| 1293 |
{ |
| 1294 |
|
| 1295 |
|
| 1296 |
OSMemElemDescr* pElem = (OSMemElemDescr*) pMemBlk->data; |
| 1297 |
for (; pElem != 0; pElem = GETNEXT (pElem)) { |
| 1298 |
|
| 1299 |
void* curMem_p = (void*) pElem_data (pElem); |
| 1300 |
if (curMem_p == mem_p && !ISFREE (pElem)) { |
| 1301 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1302 |
return 1; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1310 |
return 0; |
| 1311 |
} |
| 1312 |
|
| 1313 |
void memHeapSetProperty (void** ppvMemHeap, ASN1UINT propId, void* pProp) |
| 1314 |
{ |
| 1315 |
OSMemHeap* pMemHeap; |
| 1316 |
|
| 1317 |
if (ppvMemHeap == 0) |
| 1318 |
return; |
| 1319 |
|
| 1320 |
if (*ppvMemHeap == 0) |
| 1321 |
memHeapCreate (ppvMemHeap); |
| 1322 |
|
| 1323 |
pMemHeap = *(OSMemHeap**)ppvMemHeap; |
| 1324 |
ast_mutex_lock(&pMemHeap->pLock); |
| 1325 |
|
| 1326 |
switch (propId) { |
| 1327 |
case OSRTMH_PROPID_DEFBLKSIZE: |
| 1328 |
pMemHeap->defBlkSize = *(ASN1UINT*)pProp; |
| 1329 |
break; |
| 1330 |
case OSRTMH_PROPID_SETFLAGS: |
| 1331 |
pMemHeap->flags |= ((*(ASN1UINT*)pProp) & (~RT_MH_INTERNALMASK)); |
| 1332 |
break; |
| 1333 |
case OSRTMH_PROPID_CLEARFLAGS: |
| 1334 |
pMemHeap->flags &= ((~(*(ASN1UINT*)pProp)) | RT_MH_INTERNALMASK); |
| 1335 |
break; |
| 1336 |
} |
| 1337 |
ast_mutex_unlock(&pMemHeap->pLock); |
| 1338 |
} |
| 1339 |
|
| 1340 |
int memHeapCreate (void** ppvMemHeap) |
| 1341 |
{ |
| 1342 |
OSMemHeap* pMemHeap; |
| 1343 |
if (ppvMemHeap == 0) return ASN_E_INVPARAM; |
| 1344 |
|
| 1345 |
pMemHeap = ast_malloc(sizeof (OSMemHeap)); |
| 1346 |
if (pMemHeap == NULL) return ASN_E_NOMEM; |
| 1347 |
memset (pMemHeap, 0, sizeof (OSMemHeap)); |
| 1348 |
pMemHeap->defBlkSize = g_defBlkSize; |
| 1349 |
pMemHeap->refCnt = 1; |
| 1350 |
pMemHeap->flags = RT_MH_FREEHEAPDESC; |
| 1351 |
ast_mutex_init(&pMemHeap->pLock); |
| 1352 |
*ppvMemHeap = (void*)pMemHeap; |
| 1353 |
return ASN_OK; |
| 1354 |
} |