📄 mem5xx.c
字号:
current = current->size_ptr;
if (current == NIL) return (char *) 0;
oldsize = current->packet_size; /* REMEMBER OLD SIZE */
mremove(current); /* REMOVE PACKET FROM FREE LIST */
/******************************************************************/
/* IF PACKET IS LARGER THAN NEEDED, FREE EXTRA SPACE AT END */
/* BY INSERTING REMAINING SPACE INTO FREE LIST. */
/******************************************************************/
if (size - oldsize >= 2)
{
PACKET *next = (PACKET *) ((char *) current + 2 - size);
next->packet_size = oldsize - size + 2;
minsert(next);
current->packet_size = -size;
}
else current->packet_size = -oldsize;
return (char *) &(current->size_ptr);
}
/*****************************************************************************/
/* */
/* CALLOC - Allocate a packet of a given size, set the data in the packet */
/* to nulls, and return a pointer to it. */
/* */
/*****************************************************************************/
void *calloc(size_t num, size_t size)
{
size_t i;
int *current;
char *save;
i = size;
i = i * num;
current = (int *)malloc(i);
save = (char *)current;
if (current == 0) return 0;
while (i--) *current++ = 0;
return save;
}
/*****************************************************************************/
/* */
/* REALLOC - Reallocate a packet to a new size. */
/* */
/*****************************************************************************/
void *realloc(void *packet, size_t usersize)
{
long newsize = usersize;
char *pptr = (char *) packet - 2; /* PTR TO START OF PACKET */
long oldsize;
/****************************************************************/
/* HANDLE SPECIAL CASES */
/****************************************************************/
if (newsize < 0) return(0);
if (packet == 0) return(malloc(newsize));
if (newsize == 0) { free(packet); return (0); }
oldsize = ((PACKET *)pptr)->packet_size;
if (oldsize <= 0) return(0);
/****************************************************************/
/* ROUND SIZE UP TO EVEN BOUNDARY */
/****************************************************************/
if ((newsize & 1) == 1) ++newsize;
/****************************************************************/
/* IF NEW SIZE IS LESS THAN CURRENT PACKET SIZE, */
/* TRUNCATE THE PACKET AND RETURN END TO FREE LIST */
/****************************************************************/
if (newsize <= oldsize)
{
if (oldsize - newsize < 2) return(packet);
((PACKET *)pptr)->packet_size = newsize; /* SET NEW PACKET SIZE */
pptr += newsize + 2; /* SET PTR TO NEW PACKET */
((PACKET *)pptr)->packet_size = oldsize - newsize - 2;
free(pptr + 2); /* FREE TRAILING PACKET */
return(packet);
}
/****************************************************************/
/* IF NEW SIZE IS BIGGER THAN CURRENT PACKET, */
/* 1) CHECK NEXT PACKET IN LIST, SEE IF PACKET CAN BE EXPANDED */
/* 2) IF NOT, MOVE PACKET TO NEW LOCATION. */
/****************************************************************/
else
{
PACKET *next = (PACKET *)(pptr + oldsize + 2);
long temp;
if (((int *)next < &_sys_memory[MEMORY_SIZE - 3]) &&
((next->packet_size < 0) &&
((temp = oldsize - next->packet_size + 2 - newsize) >= 0)))
{
mremove(next);
if (temp < 3)
{
((PACKET *)pptr)->packet_size = newsize + temp;
return(packet);
}
/*********************************************************/
/* FREE TRAILING PACKET */
/*********************************************************/
((PACKET *)pptr)->packet_size = newsize;
pptr += newsize + 2;
((PACKET *)pptr)->packet_size = -(temp - 2);
minsert((PACKET *)pptr);
return(packet);
}
else
{
/************************************************************/
/* ALLOCATE NEW PACKET AND MOVE DATA INTO IT. */
/************************************************************/
char *new = malloc(newsize);
if (new == 0) return (0);
memmove(new, pptr+2, oldsize);
free(packet);
return(new);
}
}
}
/*****************************************************************************/
/* */
/* FREE - Return a packet allocated by malloc to free memory pool. */
/* */
/*****************************************************************************/
void free(void *userptr)
{
char *ptr = userptr;
PACKET *last; /* POINTER TO PREVIOUS PACKET */
PACKET *current; /* POINTER TO THIS PACKET */
PACKET *next; /* POINTER TO NEXT PACKET */
if (!ptr) return;
last = next = NIL; /* INITIALIZE POINTERS */
ptr -= 2; /* ADJUST POINT TO BEGINNING OF PACKET */
current = (PACKET *) _sys_memory;
/*********************************************************************/
/* SEARCH FOR THE POINTER IN THE PACKET POINTED TO */
/*********************************************************************/
while (current < (PACKET *) ptr)
{
long tempsize = current->packet_size;
last = current;
if (tempsize < 0) tempsize = -tempsize;
current = (PACKET *)((char *)current + tempsize + 2);
}
/*********************************************************************/
/* CHECK FOR POINTER OR PACKET ERRORS. */
/*********************************************************************/
if ((current != (PACKET *) ptr) || (current->packet_size < 0))
return;
current->packet_size = -current->packet_size; /* MARK PACKET AS FREE */
/*********************************************************************/
/* GET POINTER TO NEXT PACKET IN MEMORY, IF ANY. */
/*********************************************************************/
next = (PACKET *) ((char *)current - current->packet_size + 2);
if (next > (PACKET *) &_sys_memory[MEMORY_SIZE - 2]) next = NIL;
/*********************************************************************/
/* ATTEMPT TO COALESCE THE THREE PACKETS (PREVIOUS, CURRENT, NEXT) */
/*********************************************************************/
if ((last != NIL) && (last->packet_size < 0))
{
if ((next != NIL) && (next->packet_size < 0))
{
mremove(last);
mremove(next);
last->packet_size += next->packet_size + current->packet_size - 4;
minsert(last);
}
else
{
mremove(last);
last->packet_size += current->packet_size - 2;
minsert(last);
}
}
/*********************************************************************/
/* ATTEMPT TO COALESCE THE CURRENT WITH NEXT PACKET. (CURRENT, NEXT) */
/*********************************************************************/
else if ((next != NIL) && (next->packet_size < 0))
{
mremove(next);
current->packet_size += next->packet_size - 2;
minsert(current);
}
/*********************************************************************/
/* NO COALESCENCE POSSIBLE, JUST INSERT THIS PACKET INTO LIST */
/*********************************************************************/
else minsert(current);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -