Exemplo n.º 1
0
// hash_func_t参数不再使用,保留仅是接口兼容.
// typedef void (*dtor_func_t)(void *pDest);
ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
{
	uint i = 3;
	Bucket **tmp;

	SET_INCONSISTENT(HT_OK);

	if (nSize >= 0x80000000) {
		/* prevent overflow */
		ht->nTableSize = 0x80000000;
	} else {
		// round操作
		// i有最小值的
		while ((1U << i) < nSize) {
			i++;
		}
		ht->nTableSize = 1 << i;
	}

	ht->nTableMask = ht->nTableSize - 1;
	// 通用的元素释放函数
	ht->pDestructor = pDestructor;
	ht->arBuckets = NULL;
	ht->pListHead = NULL;
	ht->pListTail = NULL;
	ht->nNumOfElements = 0;
	ht->nNextFreeElement = 0;
	ht->pInternalPointer = NULL;
	ht->persistent = persistent;
	ht->nApplyCount = 0;
	ht->bApplyProtection = 1;
	
	/* Uses ecalloc() so that Bucket* == NULL */
	if (persistent) {
		tmp = (Bucket **) calloc(ht->nTableSize, sizeof(Bucket *));
		if (!tmp) {
			return FAILURE;
		}
		ht->arBuckets = tmp;
	} else {
		tmp = (Bucket **) ecalloc_rel(ht->nTableSize, sizeof(Bucket *));
		if (tmp) {
			ht->arBuckets = tmp;
		}
	}
	
	return SUCCESS;
}
Exemplo n.º 2
0
/* {{{ _mysqlnd_ecalloc */
static void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
	void *ret;
	zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
#if PHP_DEBUG
	zend_long * threshold = &MYSQLND_G(debug_ecalloc_fail_threshold);
#endif
	TRACE_ALLOC_ENTER(mysqlnd_ecalloc_name);

#if PHP_DEBUG
	{
		char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
		TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
	}
#endif
	TRACE_ALLOC_INF_FMT("before: %lu", zend_memory_usage(FALSE));

#if PHP_DEBUG
	/* -1 is also "true" */
	if (*threshold) {
#endif
		ret = ecalloc_rel(nmemb, REAL_SIZE(size));
#if PHP_DEBUG
		--*threshold;
	} else if (*threshold == 0) {
		ret = NULL;
	}
#endif

	TRACE_ALLOC_INF_FMT("after : %lu", zend_memory_usage(FALSE));
	TRACE_ALLOC_INF_FMT("size=%lu ptr=%p", size, ret);
	if (ret && collect_memory_statistics) {
		*(size_t *) ret = size;
		MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_ECALLOC_COUNT, 1, STAT_MEM_ECALLOC_AMOUNT, size);
	}
	TRACE_ALLOC_RETURN(FAKE_PTR(ret));
}