⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cl_malloc.c

📁 linux集群服务器软件代码包
💻 C
📖 第 1 页 / 共 2 页
字号:
		DUMPIFASKED();		return;	}	bucket = bhdr->hdr.bucket;#ifdef HA_MALLOC_MAGIC	bhdr->hdr.magic = HA_FREE_MAGIC;#endif	/*	 * Return it to the appropriate bucket (linked list), or just free	 * it if it didn't come from one of our lists...	 */	if (bucket >= NUMBUCKS) {		if (memstats) {			if (memstats->nbytes_alloc >= bhdr->hdr.reqsize) {				memstats->nbytes_req   -= bhdr->hdr.reqsize;				memstats->nbytes_alloc -= bhdr->hdr.reqsize;				memstats->mallocbytes  -= bhdr->hdr.reqsize;			}		}		free(bhdr);	}else{		int	bucksize = cl_bucket_sizes[bucket];#if defined(USE_ASSERTS)		g_assert(bhdr->hdr.reqsize <= cl_bucket_sizes[bucket]);#endif		if (memstats) {			if (memstats->nbytes_alloc >= bhdr->hdr.reqsize) {				memstats->nbytes_req  -= bhdr->hdr.reqsize;				memstats->nbytes_alloc-= bucksize;			}		}		bhdr->next = cl_malloc_buckets[bucket];		cl_malloc_buckets[bucket] = bhdr;#ifdef MARK_PRISTINE		cl_mark_pristine(ptr, bucksize);#endif	}	if (memstats) {		memstats->numfree++;	}}void*cl_realloc(void *ptr, size_t newsize){	struct cl_bucket*	bhdr;	int			bucket;	int			bucksize;	if (!cl_malloc_inityet) {		cl_malloc_init();	}	if (memstats) {		memstats->numrealloc++;	}	if (ptr == NULL) {		/* NULL is a legal 'ptr' value for realloc... */		return cl_malloc(newsize);	}	/* Find the beginning of our "hidden" structure */	bhdr = BHDR(ptr);#ifdef HA_MALLOC_MAGIC	switch (bhdr->hdr.magic) {		case HA_MALLOC_MAGIC:			break;		case HA_FREE_MAGIC:			cl_log(LOG_ERR			,	"cl_realloc: attempt to realloc already-freed"			" object at 0x%lx"			,	(unsigned long)ptr);			cl_dump_item(bhdr);			DUMPIFASKED();			return NULL;			break;		default:			cl_log(LOG_ERR, "cl_realloc: Bad magic number"			" in object at 0x%lx"			,	(unsigned long)ptr);			cl_dump_item(bhdr);			DUMPIFASKED();			return NULL;			break;	}#endif	CHECK_GUARD_BYTES(ptr, "cl_realloc");	bucket = bhdr->hdr.bucket;	/*	 * Figure out which bucket it came from... If any...	 */	if (bucket >= NUMBUCKS) {		/* Not from our bucket-area... Call realloc... */		if (memstats) {			if (memstats->nbytes_alloc >= bhdr->hdr.reqsize) {				memstats->nbytes_req   -= bhdr->hdr.reqsize;				memstats->nbytes_alloc -= bhdr->hdr.reqsize;				memstats->mallocbytes  -= bhdr->hdr.reqsize;			}			memstats->nbytes_req   += newsize;			memstats->nbytes_alloc += newsize;			memstats->mallocbytes  += newsize;		}		bhdr = realloc(bhdr, newsize + cl_malloc_hdr_offset + GUARDSIZE);		if (!bhdr) {			return NULL;		}		bhdr->hdr.reqsize = newsize;		ptr = (((char*)bhdr)+cl_malloc_hdr_offset);		ADD_GUARD(ptr);		CHECK_GUARD_BYTES(ptr, "cl_realloc - real realloc return value");		/* Not really a  memory leak...  BEAM thinks so though... */		return ptr; /*memory leak*/	}	bucksize = cl_bucket_sizes[bucket];#if defined(USE_ASSERTS)	g_assert(bhdr->hdr.reqsize <= bucksize);#endif	if (newsize > bucksize) {		/* Need to allocate new space for it */		void* newret = cl_malloc(newsize);		if (newret != NULL) {			memcpy(newret, ptr, bhdr->hdr.reqsize);			CHECK_GUARD_BYTES(newret, "cl_realloc - cl_malloc case");		}		cl_free(ptr);		return newret;	}	/* Amazing! It fits into the space previously allocated for it! */	bhdr->hdr.reqsize = newsize;	if (memstats) {		if (memstats->nbytes_alloc >= bhdr->hdr.reqsize) {			memstats->nbytes_req  -= bhdr->hdr.reqsize;		}		memstats->nbytes_req  += newsize;	}	ADD_GUARD(ptr);	CHECK_GUARD_BYTES(ptr, "cl_realloc - fits in existing space");	return ptr;}/* * cl_new_mem:	use the real malloc to allocate some new memory */static void*cl_new_mem(size_t size, int numbuck){	struct cl_bucket*	hdrret;	size_t			allocsize;	size_t			mallocsize;	if (numbuck < NUMBUCKS) {		allocsize = cl_bucket_sizes[numbuck];	}else{		allocsize = size;	}	mallocsize = allocsize + cl_malloc_hdr_offset + GUARDSIZE;	if ((hdrret = malloc(mallocsize)) == NULL) {		return NULL;	}	hdrret->hdr.reqsize = size;	hdrret->hdr.bucket = numbuck;#ifdef HA_MALLOC_MAGIC	hdrret->hdr.magic = HA_MALLOC_MAGIC;#endif	if (memstats) {		memstats->nbytes_alloc += mallocsize;		memstats->nbytes_req += size;		memstats->mallocbytes += mallocsize;	}	/* BEAM BUG -- this is NOT a leak */	return(((char*)hdrret)+cl_malloc_hdr_offset); /*memory leak*/}/* * cl_calloc: calloc clone */void *cl_calloc(size_t nmemb, size_t size){	void *	ret = cl_malloc(nmemb*size);	if (ret != NULL) {		memset(ret, 0, nmemb*size);	}			return(ret);}/* * cl_strdup: strdup clone */char *cl_strdup(const char *s){	void * ret = cl_malloc((strlen(s) + 1) * sizeof(char));	if (ret) {		strcpy(ret, s);	}			return(ret);}/* * cl_malloc_init():	initialize our malloc wrapper things */static voidcl_malloc_init(){	int	j;	size_t	cursize = 32;	cl_malloc_inityet = 1;	if (cl_malloc_hdr_offset < sizeof(long long)) {		cl_malloc_hdr_offset = sizeof(long long);	}	for (j=0; j < NUMBUCKS; ++j) {		cl_malloc_buckets[j] = NULL;		cl_bucket_sizes[j] = cursize;		cursize <<= 1;	}#ifdef MARK_PRISTINE	{		struct cl_bucket	b;		pristoff = (unsigned char*)&(b.next)-(unsigned char*)&b;		pristoff += sizeof(b.next);	}#endif}void cl_malloc_setstats(volatile cl_mem_stats_t *stats){	memstats = stats;}static voidcl_dump_item(const struct cl_bucket*b){	const unsigned char *	cbeg;	const unsigned char *	cend;	const unsigned char *	cp;	cl_log(LOG_INFO, "Dumping cl_malloc item @ 0x%lx, bucket address: 0x%lx"	,	((unsigned long)b)+cl_malloc_hdr_offset, (unsigned long)b);#ifdef HA_MALLOC_MAGIC	cl_log(LOG_INFO, "Magic number: 0x%lx reqsize=%ld"	", bucket=%d, bucksize=%ld"	,	b->hdr.magic	,	(long)b->hdr.reqsize, b->hdr.bucket	,	(long)(b->hdr.bucket >= NUMBUCKS ? 0 	:	cl_bucket_sizes[b->hdr.bucket]));#else	cl_log(LOG_INFO, "reqsize=%ld"	", bucket=%d, bucksize=%ld"	,	(long)b->hdr.reqsize, b->hdr.bucket	,	(long)(b->hdr.bucket >= NUMBUCKS ? 0 	:	cl_bucket_sizes[b->hdr.bucket]));#endif	cbeg = ((const char *)b)+cl_malloc_hdr_offset;	cend = cbeg+b->hdr.reqsize+GUARDSIZE;	for (cp=cbeg; cp < cend; cp+= sizeof(unsigned)) {		cl_log(LOG_INFO, "%02x %02x %02x %02x \"%c%c%c%c\""		,	(unsigned)cp[0], (unsigned)cp[1]		,	(unsigned)cp[2], (unsigned)cp[3]		,	cp[0], cp[1], cp[2], cp[3]);	}}/* The only reason these functions exist is because glib uses non-standard * types (gsize)in place of size_t.  Since size_t is 64-bits on some * machines where gsize (unsigned int) is 32-bits, this is annoying. */static gpointercl_malloc_glib(gsize n_bytes){	return (gpointer)cl_malloc((size_t)n_bytes);}static voidcl_free_glib(gpointer mem){	cl_free((void*)mem);}static void *cl_realloc_glib(gpointer mem, gsize n_bytes){	return cl_realloc((void*)mem, (size_t)n_bytes);}/* Call before using any glib functions(!) *//* See also: g_mem_set_vtable() */voidcl_malloc_forced_for_glib(void){	static GMemVTable vt = {		cl_malloc_glib,		cl_realloc_glib,		cl_free_glib,		NULL,		NULL,		NULL,	};	if (!cl_malloc_inityet) {		cl_malloc_init();	}	g_mem_set_vtable(&vt);}#ifdef MARK_PRISTINEstatic intcl_check_is_pristine(const void* v, unsigned size){	const unsigned char *	cp;	const unsigned char *	last;	cp = v;	last = cp + size;	cp += pristoff;	for (;cp < last; ++cp) {		if (*cp != PRISTVALUE) {			return FALSE;		}	}	return TRUE;}static voidcl_mark_pristine(void* v, unsigned size){	unsigned char *	cp = v;	memset(cp+pristoff, PRISTVALUE, size-pristoff);}#endif

⌨️ 快捷键说明

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