2002-06-17 01:20:15 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2006-01-05 07:53:05 +08:00
|
|
|
| Copyright (c) 1998-2006 Zend Technologies Ltd. (http://www.zend.com) |
|
2002-06-17 01:20:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2002-06-17 01:20:15 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-01 09:49:15 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
2002-07-25 02:53:19 +08:00
|
|
|
#include "zend.h"
|
2002-06-17 01:17:47 +08:00
|
|
|
#include "zend_mm.h"
|
|
|
|
|
|
|
|
#if WIN32|WINNT
|
|
|
|
# ifndef inline
|
|
|
|
# define inline __inline
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ZEND_MM_FREE_BLOCK 0
|
|
|
|
#define ZEND_MM_USED_BLOCK 1
|
|
|
|
|
2002-06-20 00:13:39 +08:00
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) (((a)>(b))?(a):(b))
|
|
|
|
#endif
|
2002-06-17 01:17:47 +08:00
|
|
|
|
2002-07-25 02:53:19 +08:00
|
|
|
#ifndef ZEND_MM_ALIGNMENT
|
2002-06-24 21:21:49 +08:00
|
|
|
#define ZEND_MM_ALIGNMENT 8
|
2002-07-25 02:53:19 +08:00
|
|
|
#define ZEND_MM_ALIGNMENT_LOG2 3
|
2002-07-25 01:04:11 +08:00
|
|
|
#endif
|
2002-06-24 21:21:49 +08:00
|
|
|
|
2002-06-18 03:31:14 +08:00
|
|
|
#define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
|
2002-06-17 01:17:47 +08:00
|
|
|
|
2002-07-25 02:53:19 +08:00
|
|
|
#define ZEND_MM_BUCKET_INDEX(true_size) (true_size >> ZEND_MM_ALIGNMENT_LOG2)
|
2002-06-24 20:39:08 +08:00
|
|
|
|
2002-06-17 01:17:47 +08:00
|
|
|
/* Aligned header size */
|
2002-06-18 03:31:14 +08:00
|
|
|
#define ZEND_MM_ALIGNED_SIZE(size) ((size + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
|
2002-06-17 01:17:47 +08:00
|
|
|
#define ZEND_MM_ALIGNED_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block))
|
2002-06-18 02:24:08 +08:00
|
|
|
#define ZEND_MM_ALIGNED_FREE_HEADER_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_free_block))
|
2002-06-18 03:00:37 +08:00
|
|
|
#define ZEND_MM_ALIGNED_SEGMENT_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
|
2002-06-17 01:17:47 +08:00
|
|
|
|
|
|
|
/* Memory calculations */
|
|
|
|
#define ZEND_MM_BLOCK_AT(blk, offset) ((zend_mm_block *) (((char *) (blk))+(offset)))
|
|
|
|
#define ZEND_MM_DATA_OF(p) ((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))
|
|
|
|
#define ZEND_MM_HEADER_OF(blk) ZEND_MM_BLOCK_AT(blk, -(int)ZEND_MM_ALIGNED_HEADER_SIZE)
|
|
|
|
|
|
|
|
/* Debug output */
|
|
|
|
#define ZEND_MM_DEBUG(stmt)
|
|
|
|
|
|
|
|
|
2003-07-04 23:38:12 +08:00
|
|
|
/* Start Heap Code */
|
|
|
|
|
|
|
|
static int left_child[ZEND_HEAP_MAX_BUCKETS];
|
|
|
|
static int left_child_of_right_brother[ZEND_HEAP_MAX_BUCKETS];
|
|
|
|
|
|
|
|
static inline void zend_heap_init(zend_heap heap)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<ZEND_HEAP_MAX_BUCKETS; i++) {
|
|
|
|
left_child[i] = 2*i+1;
|
|
|
|
left_child_of_right_brother[i] = 2*i+3;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(heap, 0, sizeof(zend_heap));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void zend_heap_activate_leaf(zend_heap heap, int node)
|
|
|
|
{
|
|
|
|
int i = node + ZEND_HEAP_MAX_BUCKETS-1;
|
|
|
|
|
|
|
|
heap[i] = node;
|
|
|
|
|
|
|
|
do {
|
|
|
|
i = (i-1)>>1; /* (i-1)/2 */
|
|
|
|
if (heap[i] < node) {
|
|
|
|
heap[i] = node;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (i > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void zend_heap_deactivate_leaf(zend_heap heap, int node)
|
|
|
|
{
|
|
|
|
int i = node + ZEND_HEAP_MAX_BUCKETS-1;
|
|
|
|
|
|
|
|
heap[i] = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
i = (i-1)>>1; /* (i-1)/2 */
|
|
|
|
if (heap[i] == node) {
|
|
|
|
heap[i] = MAX(heap[2*i+1], heap[2*i+2]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (i > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int zend_heap_search_leaf(zend_heap heap, int value)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if (heap[0] < value) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (i < ZEND_HEAP_MAX_BUCKETS) {
|
|
|
|
if (heap[i] >= value) {
|
|
|
|
/* Go to left child */
|
|
|
|
i = left_child[i];
|
|
|
|
} else {
|
|
|
|
/* Go to left child of right brother */
|
|
|
|
i = left_child_of_right_brother[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (heap[i] >= value) {
|
|
|
|
return heap[i];
|
|
|
|
} else {
|
|
|
|
return heap[i+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Heap Code */
|
|
|
|
|
2002-06-18 02:24:08 +08:00
|
|
|
static inline void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
|
2002-06-17 01:17:47 +08:00
|
|
|
{
|
2002-06-24 20:39:08 +08:00
|
|
|
zend_mm_free_block **free_list_bucket;
|
|
|
|
size_t index = ZEND_MM_BUCKET_INDEX(mm_block->size);
|
|
|
|
|
2003-07-04 23:38:12 +08:00
|
|
|
if (index < ZEND_MM_NUM_BUCKETS) {
|
|
|
|
free_list_bucket = &heap->free_buckets[index];
|
|
|
|
if (*free_list_bucket == NULL) {
|
|
|
|
zend_heap_activate_leaf(heap->heap, index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free_list_bucket = &heap->free_buckets[0];
|
|
|
|
}
|
2002-06-24 20:39:08 +08:00
|
|
|
mm_block->next_free_block = *free_list_bucket;
|
2003-07-04 23:38:12 +08:00
|
|
|
if (*free_list_bucket != NULL) {
|
2002-06-17 01:17:47 +08:00
|
|
|
mm_block->next_free_block->prev_free_block = mm_block;
|
|
|
|
}
|
2003-07-04 23:38:12 +08:00
|
|
|
*free_list_bucket = mm_block;
|
|
|
|
mm_block->prev_free_block = NULL;
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-18 02:24:08 +08:00
|
|
|
static inline void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
|
2002-06-17 01:17:47 +08:00
|
|
|
{
|
|
|
|
if (mm_block->prev_free_block) {
|
|
|
|
mm_block->prev_free_block->next_free_block = mm_block->next_free_block;
|
|
|
|
} else {
|
2002-06-24 20:39:08 +08:00
|
|
|
zend_mm_free_block **free_list_bucket;
|
|
|
|
size_t index = ZEND_MM_BUCKET_INDEX(mm_block->size);
|
|
|
|
|
2003-07-04 23:38:12 +08:00
|
|
|
if (index < ZEND_MM_NUM_BUCKETS) {
|
|
|
|
free_list_bucket = &heap->free_buckets[index];
|
|
|
|
*free_list_bucket = mm_block->next_free_block;
|
|
|
|
if (*free_list_bucket == NULL) {
|
|
|
|
zend_heap_deactivate_leaf(heap->heap, index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free_list_bucket = &heap->free_buckets[0];
|
|
|
|
*free_list_bucket = mm_block->next_free_block;
|
|
|
|
}
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mm_block->next_free_block) {
|
|
|
|
mm_block->next_free_block->prev_free_block = mm_block->prev_free_block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-23 18:46:27 +08:00
|
|
|
static inline void zend_mm_create_new_free_block(zend_mm_heap *heap, zend_mm_block *mm_block, size_t true_size)
|
|
|
|
{
|
|
|
|
int remaining_size;
|
|
|
|
zend_mm_free_block *new_free_block;
|
|
|
|
|
|
|
|
/* calculate sizes */
|
|
|
|
remaining_size = mm_block->size - true_size;
|
|
|
|
|
|
|
|
if (remaining_size < ZEND_MM_ALIGNED_FREE_HEADER_SIZE) {
|
|
|
|
/* keep best_fit->size as is, it'll include this extra space */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare new free block */
|
|
|
|
mm_block->size = true_size;
|
|
|
|
new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
|
|
|
|
new_free_block->type = ZEND_MM_FREE_BLOCK;
|
|
|
|
new_free_block->size = remaining_size;
|
|
|
|
new_free_block->prev_size = true_size;
|
|
|
|
|
|
|
|
/* update the next block's prev_size */
|
|
|
|
ZEND_MM_BLOCK_AT(new_free_block, new_free_block->size)->prev_size = new_free_block->size;
|
|
|
|
|
|
|
|
/* add the new free block to the free list */
|
|
|
|
zend_mm_add_to_free_list(heap, new_free_block);
|
|
|
|
}
|
2002-06-17 01:17:47 +08:00
|
|
|
|
|
|
|
zend_bool zend_mm_add_memory_block(zend_mm_heap *heap, size_t block_size)
|
|
|
|
{
|
2002-06-18 03:00:37 +08:00
|
|
|
/* We need to make sure that block_size is big enough for the minimum segment size */
|
2002-06-18 02:24:08 +08:00
|
|
|
zend_mm_free_block *mm_block;
|
2002-06-17 01:17:47 +08:00
|
|
|
zend_mm_block *guard_block;
|
2002-06-18 03:00:37 +08:00
|
|
|
zend_mm_segment *segment;
|
2002-06-17 01:17:47 +08:00
|
|
|
|
|
|
|
/* align block size downwards */
|
2002-06-18 03:31:14 +08:00
|
|
|
block_size = block_size & ZEND_MM_ALIGNMENT_MASK;
|
2002-06-17 01:17:47 +08:00
|
|
|
|
2002-06-18 03:00:37 +08:00
|
|
|
segment = (zend_mm_segment *) malloc(block_size);
|
|
|
|
if (!segment) {
|
2002-06-17 01:17:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-06-23 00:09:15 +08:00
|
|
|
/* fprintf(stderr, "Allocating segment %X\n", segment); */
|
2002-06-18 03:00:37 +08:00
|
|
|
segment->next_segment = heap->segments_list;
|
|
|
|
heap->segments_list = segment;
|
|
|
|
|
|
|
|
block_size -= ZEND_MM_ALIGNED_SEGMENT_SIZE;
|
|
|
|
mm_block = (zend_mm_free_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
|
|
|
|
|
2002-12-09 16:14:00 +08:00
|
|
|
mm_block->size = block_size - ZEND_MM_ALIGNED_HEADER_SIZE; /* keep one guard block in the end */
|
2002-06-17 01:17:47 +08:00
|
|
|
mm_block->type = ZEND_MM_FREE_BLOCK;
|
|
|
|
mm_block->prev_size = 0; /* Size is always at least ZEND_MM_ALIGNED_HEADER_SIZE big (>0) so 0 is OK */
|
|
|
|
|
|
|
|
/* setup guard block */
|
|
|
|
guard_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
guard_block->type = ZEND_MM_USED_BLOCK;
|
2002-12-09 16:14:00 +08:00
|
|
|
guard_block->size = ZEND_MM_ALIGNED_HEADER_SIZE;
|
|
|
|
guard_block->guard_block = 1;
|
2002-06-17 01:17:47 +08:00
|
|
|
guard_block->prev_size = mm_block->size;
|
|
|
|
ZEND_MM_DEBUG(("Setup guard block at 0x%0.8X\n", guard_block));
|
|
|
|
|
|
|
|
zend_mm_add_to_free_list(heap, mm_block);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notes:
|
|
|
|
* - This function may alter the block_sizes values to match platform alignment
|
|
|
|
* - This function does *not* perform sanity checks on the arguments
|
|
|
|
*/
|
|
|
|
zend_bool zend_mm_startup(zend_mm_heap *heap, size_t block_size)
|
|
|
|
{
|
|
|
|
heap->block_size = block_size;
|
2002-06-18 03:00:37 +08:00
|
|
|
heap->segments_list = NULL;
|
2002-06-24 20:39:08 +08:00
|
|
|
memset(heap->free_buckets, 0, sizeof(heap->free_buckets));
|
2003-07-04 23:38:12 +08:00
|
|
|
zend_heap_init(heap->heap);
|
2002-06-18 02:24:08 +08:00
|
|
|
return zend_mm_add_memory_block(heap, block_size);
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void zend_mm_shutdown(zend_mm_heap *heap)
|
|
|
|
{
|
2002-06-18 03:00:37 +08:00
|
|
|
zend_mm_segment *segment = heap->segments_list;
|
|
|
|
zend_mm_segment *prev;
|
|
|
|
|
|
|
|
while (segment) {
|
|
|
|
prev = segment;
|
|
|
|
segment = segment->next_segment;
|
2002-06-23 00:09:15 +08:00
|
|
|
/* fprintf(stderr, "Freeing segment %X\n", prev);*/
|
2002-06-18 03:00:37 +08:00
|
|
|
free(prev);
|
|
|
|
}
|
2002-06-23 00:09:15 +08:00
|
|
|
heap->segments_list = NULL;
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *zend_mm_alloc(zend_mm_heap *heap, size_t size)
|
|
|
|
{
|
|
|
|
size_t true_size;
|
2002-06-23 18:46:27 +08:00
|
|
|
zend_mm_free_block *p, *best_fit=NULL;
|
2002-06-24 20:39:08 +08:00
|
|
|
zend_mm_free_block **free_list_bucket;
|
|
|
|
size_t index;
|
2002-06-17 01:17:47 +08:00
|
|
|
|
2003-07-01 04:22:35 +08:00
|
|
|
/* The max() can probably be optimized with an if () which checks more specific cases */
|
2002-06-20 00:13:39 +08:00
|
|
|
true_size = MAX(ZEND_MM_ALIGNED_SIZE(size)+ZEND_MM_ALIGNED_HEADER_SIZE, ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
|
2002-06-24 20:39:08 +08:00
|
|
|
|
|
|
|
index = ZEND_MM_BUCKET_INDEX(true_size);
|
|
|
|
|
|
|
|
if (index < ZEND_MM_NUM_BUCKETS) {
|
2003-07-04 23:38:12 +08:00
|
|
|
free_list_bucket = &heap->free_buckets[index];
|
|
|
|
if (*free_list_bucket) {
|
|
|
|
best_fit = *free_list_bucket;
|
|
|
|
goto zend_mm_finished_searching_for_block;
|
|
|
|
} else {
|
|
|
|
int leaf;
|
|
|
|
|
|
|
|
leaf = zend_heap_search_leaf(heap->heap, index);
|
|
|
|
if (leaf != 0) {
|
|
|
|
best_fit = heap->free_buckets[leaf];
|
2002-06-24 20:39:08 +08:00
|
|
|
goto zend_mm_finished_searching_for_block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = heap->free_buckets[0]; p; p = p->next_free_block) {
|
2002-06-17 01:17:47 +08:00
|
|
|
if (p->size == true_size) {
|
|
|
|
best_fit = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((p->size > true_size) && (!best_fit || (best_fit->size > p->size))) { /* better fit */
|
|
|
|
best_fit = p;
|
|
|
|
}
|
|
|
|
}
|
2002-06-24 20:39:08 +08:00
|
|
|
|
|
|
|
zend_mm_finished_searching_for_block:
|
2002-06-17 01:17:47 +08:00
|
|
|
if (!best_fit) {
|
2002-12-09 16:14:00 +08:00
|
|
|
if (true_size > (heap->block_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE)) {
|
2002-06-17 01:17:47 +08:00
|
|
|
/* Make sure we add a memory block which is big enough */
|
2004-03-04 17:18:05 +08:00
|
|
|
if (zend_mm_add_memory_block(heap, true_size + ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
|
2005-03-06 23:55:41 +08:00
|
|
|
zend_error(E_ERROR, "Out of memory: cannot allocate %zd bytes!", true_size);
|
2004-03-04 17:18:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-17 01:17:47 +08:00
|
|
|
} else {
|
2004-03-04 17:18:05 +08:00
|
|
|
if (zend_mm_add_memory_block(heap, heap->block_size)) {
|
2005-03-06 23:55:41 +08:00
|
|
|
zend_error(E_ERROR, "Out of memory: cannot allocate %zd bytes!", heap->block_size);
|
2004-03-04 17:18:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
return zend_mm_alloc(heap, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark as used */
|
|
|
|
best_fit->type = ZEND_MM_USED_BLOCK;
|
2002-12-09 16:14:00 +08:00
|
|
|
((zend_mm_block *) best_fit)->guard_block = 0;
|
2002-06-17 01:17:47 +08:00
|
|
|
|
|
|
|
/* remove from free list */
|
|
|
|
zend_mm_remove_from_free_list(heap, best_fit);
|
|
|
|
|
2002-06-23 18:46:27 +08:00
|
|
|
zend_mm_create_new_free_block(heap, (zend_mm_block *) best_fit, true_size);
|
2002-06-17 01:17:47 +08:00
|
|
|
|
|
|
|
return ZEND_MM_DATA_OF(best_fit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void zend_mm_free(zend_mm_heap *heap, void *p)
|
|
|
|
{
|
|
|
|
zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
|
|
|
|
zend_mm_block *prev_block, *next_block;
|
|
|
|
|
2002-12-09 16:14:00 +08:00
|
|
|
if (mm_block->type != ZEND_MM_USED_BLOCK) {
|
2002-06-17 01:17:47 +08:00
|
|
|
/* error */
|
|
|
|
return;
|
|
|
|
}
|
2002-06-24 20:39:08 +08:00
|
|
|
|
2002-06-17 01:17:47 +08:00
|
|
|
next_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
|
|
|
|
/* merge with previous block if empty */
|
|
|
|
if (mm_block->prev_size != 0
|
|
|
|
&& (prev_block=ZEND_MM_BLOCK_AT(mm_block, -(int)mm_block->prev_size))->type == ZEND_MM_FREE_BLOCK) {
|
2002-06-24 20:39:08 +08:00
|
|
|
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) prev_block);
|
2002-06-17 01:17:47 +08:00
|
|
|
prev_block->size += mm_block->size;
|
|
|
|
mm_block = prev_block;
|
|
|
|
next_block->prev_size = mm_block->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* merge with the next block if empty */
|
|
|
|
if (next_block->type == ZEND_MM_FREE_BLOCK) {
|
|
|
|
mm_block->size += next_block->size;
|
2002-06-18 02:24:08 +08:00
|
|
|
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
|
2002-06-17 01:17:47 +08:00
|
|
|
next_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size); /* recalculate */
|
|
|
|
next_block->prev_size = mm_block->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_block->type = ZEND_MM_FREE_BLOCK;
|
2002-06-24 20:39:08 +08:00
|
|
|
zend_mm_add_to_free_list(heap, (zend_mm_free_block *) mm_block);
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size)
|
|
|
|
{
|
|
|
|
zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
|
2002-06-23 18:46:27 +08:00
|
|
|
zend_mm_block *next_block;
|
2002-06-20 00:13:39 +08:00
|
|
|
size_t true_size = MAX(ZEND_MM_ALIGNED_SIZE(size)+ZEND_MM_ALIGNED_HEADER_SIZE, ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
|
2002-06-17 01:17:47 +08:00
|
|
|
|
2003-10-14 22:23:12 +08:00
|
|
|
next_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
|
2002-06-17 01:17:47 +08:00
|
|
|
if (true_size <= mm_block->size) {
|
2002-06-23 18:46:27 +08:00
|
|
|
zend_mm_create_new_free_block(heap, mm_block, true_size);
|
|
|
|
|
2003-10-14 22:23:12 +08:00
|
|
|
if (next_block->type == ZEND_MM_FREE_BLOCK) {
|
|
|
|
zend_mm_block *new_next_block;
|
|
|
|
|
|
|
|
new_next_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
if (new_next_block != next_block) { /* A new free block was created */
|
2003-10-14 22:44:19 +08:00
|
|
|
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) new_next_block);
|
2003-10-14 22:23:12 +08:00
|
|
|
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
|
|
|
|
new_next_block->size += next_block->size;
|
|
|
|
/* update the next block's prev_size */
|
2003-10-15 02:13:55 +08:00
|
|
|
ZEND_MM_BLOCK_AT(new_next_block, new_next_block->size)->prev_size = new_next_block->size;
|
2003-10-14 22:44:19 +08:00
|
|
|
zend_mm_add_to_free_list(heap, (zend_mm_free_block *) new_next_block);
|
2003-10-14 22:23:12 +08:00
|
|
|
}
|
|
|
|
}
|
2002-06-17 01:17:47 +08:00
|
|
|
return p;
|
|
|
|
}
|
2002-06-23 18:46:27 +08:00
|
|
|
|
2002-12-09 16:14:00 +08:00
|
|
|
if ((mm_block->prev_size == 0) && (next_block->type == ZEND_MM_USED_BLOCK) &&
|
|
|
|
(next_block->guard_block)) {
|
|
|
|
zend_mm_segment *segment = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
|
|
|
|
zend_mm_segment *segment_copy = segment;
|
|
|
|
zend_mm_block *guard_block;
|
|
|
|
size_t realloc_to_size;
|
|
|
|
|
|
|
|
/* segment size, size of block and size of guard block */
|
|
|
|
realloc_to_size = ZEND_MM_ALIGNED_SEGMENT_SIZE+true_size+ZEND_MM_ALIGNED_HEADER_SIZE;
|
|
|
|
segment = realloc(segment, realloc_to_size);
|
2004-01-07 06:44:40 +08:00
|
|
|
if (!segment) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-12-09 16:27:23 +08:00
|
|
|
|
2002-12-09 16:14:00 +08:00
|
|
|
if (segment != segment_copy) {
|
2002-12-09 16:27:23 +08:00
|
|
|
if (heap->segments_list == segment_copy) {
|
|
|
|
heap->segments_list = segment;
|
|
|
|
} else {
|
|
|
|
zend_mm_segment *seg = heap->segments_list;
|
|
|
|
|
|
|
|
while (seg) {
|
|
|
|
if (seg->next_segment == segment_copy) {
|
|
|
|
seg->next_segment = segment;
|
|
|
|
break;
|
|
|
|
}
|
2002-12-09 16:31:44 +08:00
|
|
|
seg = seg->next_segment;
|
2002-12-09 16:27:23 +08:00
|
|
|
}
|
|
|
|
}
|
2002-12-09 16:36:36 +08:00
|
|
|
mm_block = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
|
2002-12-09 16:14:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mm_block->size = true_size;
|
|
|
|
|
|
|
|
/* setup guard block */
|
|
|
|
guard_block = ZEND_MM_BLOCK_AT(mm_block, mm_block->size);
|
|
|
|
guard_block->type = ZEND_MM_USED_BLOCK;
|
|
|
|
guard_block->size = ZEND_MM_ALIGNED_HEADER_SIZE;
|
|
|
|
guard_block->guard_block = 1;
|
|
|
|
guard_block->prev_size = mm_block->size;
|
|
|
|
|
|
|
|
return ZEND_MM_DATA_OF(mm_block);
|
|
|
|
}
|
|
|
|
|
2002-06-23 18:46:27 +08:00
|
|
|
if (next_block->type != ZEND_MM_FREE_BLOCK || (mm_block->size + next_block->size < true_size)) {
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = zend_mm_alloc(heap, size);
|
|
|
|
memcpy(ptr, p, mm_block->size - ZEND_MM_ALIGNED_HEADER_SIZE);
|
|
|
|
zend_mm_free(heap, p);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
|
|
|
|
mm_block->size += next_block->size;
|
2002-06-23 18:49:21 +08:00
|
|
|
/* update the next block's prev_size */
|
|
|
|
ZEND_MM_BLOCK_AT(mm_block, mm_block->size)->prev_size = mm_block->size;
|
2002-06-23 18:46:27 +08:00
|
|
|
|
|
|
|
zend_mm_create_new_free_block(heap, mm_block, true_size);
|
|
|
|
|
|
|
|
return p;
|
2002-06-17 01:17:47 +08:00
|
|
|
}
|
2003-02-01 09:49:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|