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

📄 rvallocadapt.c

📁 h.248协议源码
💻 C
字号:
/******************************************************************************
Filename: rvallocadapt.c
Description: Allocator adaptors
******************************************************************************
                Copyright (c) 1999 RADVision Inc.
************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision LTD.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVision LTD..

RADVision LTD. reserves the right to revise this publication and make 
changes without obligation to notify any person of such revisions or 
changes.
******************************************************************************
$Revision:$
$Date:$ 10.5.00
$Author: Dan Elbert
******************************************************************************/
#include "rvallocadapt.h"
#include "rvmem.h"
#include "rvutil.h"

#define RV_POOLALLOC_MAXSIZE 20000

/*------- RvPool  Adaptor ---------------------------------------------------*/

static void* rvPoolAllocAllocate(void* pool, size_t size) {
	return rvPoolAllocate((RvPool*)pool);
}

static void rvPoolAllocDeallocate(void* pool, size_t size, void* ptr) {
	rvPoolDeallocate((RvPool*)pool, ptr);
}

typedef struct {
	RvPool * pool;
} RvPoolAllocHdr;

static void* rvPoolAllocAllocateEx(void* pool, size_t size) {
	char * buf = NULL;
	RvPool * pool_ = (RvPool*)pool;
	RvPoolAllocHdr * header;

	/* If smaller than pool block size,try to allocate from pool */
	if (size + sizeof(RvPoolAllocHdr) <= rvPoolGetBlockSize((RvPool*)pool) )
		buf = (char*)rvPoolAllocate((RvPool*)pool);

	/* If allocation fails or too big, allocate dynamically */
	if (buf == NULL) {
		size = rvMin(size + sizeof(RvPoolAllocHdr), RV_POOLALLOC_MAXSIZE);
		buf = (char*)rvMemAlloc(size);
		pool_ = NULL;	
	}

	/* If all allocations fails,return NULLL */
	if (buf == NULL) 
		return NULL;

	/* Set header and return buffer */
	header = (RvPoolAllocHdr*)buf;
	header->pool = pool_;
	return buf + sizeof(RvPoolAllocHdr);
}

static void rvPoolAllocDeallocateEx(void* pool, size_t size, void* ptr) {
	void* buf = ((char*)ptr - sizeof(RvPoolAllocHdr));
	RvPoolAllocHdr* header = (RvPoolAllocHdr*)buf;
	if(header->pool != NULL)
		rvPoolDeallocate(header->pool, buf);
	else
		rvMemFree(buf);
}

/* 
Allocator adaptor for rvPool
alloc -  The allocator
rawMemForPool  -  The pool to allocate from
blockSize - The block size of the pool
blocksPerPage - The number of blocks per page the pool manages
expand - If true, allocations bigger than pool block size will 
         be dynamically allocated
*/

RvAlloc* rvPoolAllocConstruct(RvAlloc* alloc, RvPool* rawMemForPool,
  size_t blockSize, size_t blocksPerPage, RvBool expand) {
	RvAlloc* r = NULL;
	RvPool* pool;
	
	if (expand) {
		pool = rvPoolConstructEx2(rawMemForPool, 
		  blockSize + sizeof(RvPoolAllocHdr), blocksPerPage, 
		  NULL, NULL, NULL,&rvDefaultAlloc);

		if (pool)
			r = rvAllocConstruct(alloc, pool, ~0U,
			  rvPoolAllocAllocateEx, rvPoolAllocDeallocateEx);

	} else {
		pool = rvPoolConstructEx2(rawMemForPool, blockSize, 
		  blocksPerPage, NULL, NULL, NULL,&rvDefaultAlloc);

		if (pool)
			r = rvAllocConstruct(alloc, pool, rvPoolGetBlockSize(pool),
			  rvPoolAllocAllocate, rvPoolAllocDeallocate);
	}

	return r;
}

void rvPoolAllocDestruct(RvAlloc* alloc) {
	RvPool* pool = (RvPool*)rvAllocGetPool(alloc);
	rvAllocDestruct(alloc);
	rvPoolDestruct(pool);
}

/*------- Adaptor for external buffer ---------------------------------------*/

/* After first time, reset the pool in the allocator to NULL so 
   no more allocations are possible */
static void* rvBufAllocAllocate(void* pool, size_t size) {
	RvAlloc * alloc = NULL;
	if (pool != NULL) {
		memcpy(&alloc, pool, sizeof(RvAlloc*));	
		alloc->pool = NULL;
		alloc->maxSize = 0;
	}
	return pool;
}


static void rvBufAllocDeallocate(void* pool, size_t size, void* ptr) {
	/* Nothing has to be done... */
	;
}

/* Allocator adaptor using an external buffer */
/* Allocator adaptor using an external buffer */
RvAlloc* rvBufAllocConstruct(RvAlloc* alloc, void* buffer, size_t size) {
	rvAllocConstruct(alloc, buffer, size, 
	  rvBufAllocAllocate, rvBufAllocDeallocate);
	memcpy(buffer, alloc, sizeof(RvAlloc*));
	return alloc;
}

⌨️ 快捷键说明

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