malloc.c

来自「把fortran语言编的程序转为c语言编的程序, 运行环境linux」· C语言 代码 · 共 167 行

C
167
字号
/****************************************************************Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.Permission to use, copy, modify, and distribute this softwareand its documentation for any purpose and without fee is herebygranted, provided that the above copyright notice appear in allcopies and that both that the copyright notice and thispermission notice and warranty disclaimer appear in supportingdocumentation, and that the names of AT&T Bell Laboratories orBellcore or any of their entities not be used in advertising orpublicity pertaining to distribution of the software withoutspecific, written prior permission.AT&T and Bellcore disclaim all warranties with regard to thissoftware, including all implied warranties of merchantabilityand fitness.  In no event shall AT&T or Bellcore be liable forany special, indirect or consequential damages or any damageswhatsoever resulting from loss of use, data or profits, whetherin an action of contract, negligence or other tortious action,arising out of or in connection with the use or performance ofthis software.****************************************************************/#ifndef CRAY#define STACKMIN 512#define MINBLK (2*sizeof(struct mem) + 16)#define F _malloc_free_#define SBGULP 8192#include "string.h"	/* for memcpy */#ifdef KR_headers#define Char char#define Unsigned unsigned#define Int /*int*/#else#define Char void#define Unsigned size_t#define Int int#endiftypedef struct mem {	struct mem *next;	Unsigned len;	} mem;mem *F; Char *#ifdef KR_headersmalloc(size)	register Unsigned size;#elsemalloc(register Unsigned size)#endif{	register mem *p, *q, *r, *s;	unsigned register k, m;	extern Char *sbrk(Int);	char *top, *top1;	size = (size+7) & ~7;	r = (mem *) &F;	for (p = F, q = 0; p; r = p, p = p->next) {		if ((k = p->len) >= size && (!q || m > k)) {			m = k;			q = p;			s = r;			}		}	if (q) {		if (q->len - size >= MINBLK) { /* split block */			p = (mem *) (((char *) (q+1)) + size);			p->next = q->next;			p->len = q->len - size - sizeof(mem);			s->next = p;			q->len = size;			}		else			s->next = q->next;		}	else {		top = (Char *)(((long)sbrk(0) + 7) & ~7);		if (F && (char *)(F+1) + F->len == top) {			q = F;			F = F->next;			}		else			q = (mem *) top;		top1 = (char *)(q+1) + size;		if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)			return 0;		r = (mem *)top1;		r->len = SBGULP - sizeof(mem);		r->next = F;		F = r;		top1 += SBGULP;		q->len = size;		}	return (Char *) (q+1);	} void#ifdef KR_headersfree(f)	Char *f;#elsefree(Char *f)#endif{	mem *p, *q, *r;	char *pn, *qn;	if (!f) return;	q = (mem *) ((char *)f - sizeof(mem));	qn = (char *)f + q->len;	for (p = F, r = (mem *) &F; ; r = p, p = p->next) {		if (qn == (Char *) p) {			q->len += p->len + sizeof(mem);			p = p->next;			}		pn = p ? ((char *) (p+1)) + p->len : 0;		if (pn == (Char *) q) {			p->len += sizeof(mem) + q->len;			q->len = 0;			q->next = p;			r->next = p;			break;			}		if (pn < (char *) q) {			r->next = q;			q->next = p;			break;			}		}	} Char *#ifdef KR_headersrealloc(f, size)	Char *f;	Unsigned size;#elserealloc(Char *f, Unsigned size)#endif{	mem *p;	Char *q, *f1;	Unsigned s1;	if (!f) return malloc(size);	p = (mem *) ((char *)f - sizeof(mem));	s1 = p->len;	free(f);	if (s1 > size)		s1 = size + 7 & ~7;	if (!p->len) {		f1 = (Char *)(p->next + 1);		memcpy(f1, f, s1);		f = f1;		}	q = malloc(size);	if (q && q != f)		memcpy(q, f, s1);	return q;	}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?