Пример #1
0
/* allocates a new thread-safe-resource id */
TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor)
{/*{{{*/
	int i;

	TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtaining a new resource id, %d bytes", size));

	tsrm_mutex_lock(tsmm_mutex);

	/* obtain a resource id */
	*rsrc_id = TSRM_SHUFFLE_RSRC_ID(id_count++);
	TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtained resource id %d", *rsrc_id));

	/* store the new resource type in the resource sizes table */
	if (resource_types_table_size < id_count) {
		tsrm_resource_type *_tmp;
		_tmp = (tsrm_resource_type *) realloc(resource_types_table, sizeof(tsrm_resource_type)*id_count);
		if (!_tmp) {
			tsrm_mutex_unlock(tsmm_mutex);
			TSRM_ERROR((TSRM_ERROR_LEVEL_ERROR, "Unable to allocate storage for resource"));
			*rsrc_id = 0;
			return 0;
		}
		resource_types_table = _tmp;
		resource_types_table_size = id_count;
	}
	resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].size = size;
	resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].ctor = ctor;
	resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].dtor = dtor;
	resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].done = 0;

	/* enlarge the arrays for the already active threads */
	for (i=0; i<tsrm_tls_table_size; i++) {
		tsrm_tls_entry *p = tsrm_tls_table[i];

		while (p) {
			if (p->count < id_count) {
				int j;

				p->storage = (void *) realloc(p->storage, sizeof(void *)*id_count);
				for (j=p->count; j<id_count; j++) {
					p->storage[j] = (void *) malloc(resource_types_table[j].size);
					if (resource_types_table[j].ctor) {
						resource_types_table[j].ctor(p->storage[j]);
					}
				}
				p->count = id_count;
			}
			p = p->next;
		}
	}
	tsrm_mutex_unlock(tsmm_mutex);

	TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Successfully allocated new resource id %d", *rsrc_id));
	return *rsrc_id;
}/*}}}*/
Пример #2
0
/* deallocates all occurrences of a given id */
void ts_free_id(ts_rsrc_id id)
{/*{{{*/
	int i;
	int j = TSRM_UNSHUFFLE_RSRC_ID(id);

	tsrm_mutex_lock(tsmm_mutex);

	TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Freeing resource id %d", id));

	if (tsrm_tls_table) {
		for (i=0; i<tsrm_tls_table_size; i++) {
			tsrm_tls_entry *p = tsrm_tls_table[i];

			while (p) {
				if (p->count > j && p->storage[j]) {
					if (resource_types_table && resource_types_table[j].dtor) {
						resource_types_table[j].dtor(p->storage[j]);
					}
					free(p->storage[j]);
					p->storage[j] = NULL;
				}
				p = p->next;
			}
		}
	}
	resource_types_table[j].done = 1;

	tsrm_mutex_unlock(tsmm_mutex);

	TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Successfully freed resource id %d", id));
}/*}}}*/
Пример #3
0
TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor)
{
  TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtaining a new resource id, %d bytes", size));

  /* obtain a resource id */
  HPHP::resource_types_table.resize(HPHP::resource_types_table.size() + 1);
  *rsrc_id = TSRM_SHUFFLE_RSRC_ID(HPHP::resource_types_table.size() - 1);
  TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtained resource id %d", *rsrc_id));

  /* store the new resource type in the resource sizes table */
  HPHP::resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].size = size;
  HPHP::resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].ctor = ctor;
  HPHP::resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].dtor = dtor;
  HPHP::resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].done = 0;

  TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Successfully allocated new resource id %d", *rsrc_id));
  return *rsrc_id;
}
Пример #4
0
/* {{{ SUCCESS|FAILURE php_output_activate(TSRMLS_D)
 * Reset output globals and setup the output handler stack */
PHPAPI int php_output_activate(TSRMLS_D)
{
#ifdef ZTS
	memset((*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)], 0, sizeof(zend_output_globals));
#else
	memset(&output_globals, 0, sizeof(zend_output_globals));
#endif

	zend_stack_init(&OG(handlers));

	return SUCCESS;
}
Пример #5
0
void ts_free_id(ts_rsrc_id id)
{
  HPHP::TSRMStorageVector & vec = *HPHP::tsrm_thread_resources;
  int j = TSRM_UNSHUFFLE_RSRC_ID(id);
  assert(id != 0);
  TSRMLS_FETCH();
  if (j < vec.size() && vec[j] != nullptr) {
    if (HPHP::resource_types_table[j].dtor) {
      HPHP::resource_types_table[j].dtor(vec[j] TSRMLS_CC);
    }
    free(vec[j]);
    vec[j] = nullptr;
  }
}
Пример #6
0
 void * ts_init_resource(int id)
 {
   assert(id != 0);
   TSRMLS_FETCH();
   HPHP::TSRMStorageVector & vec = *HPHP::tsrm_thread_resources;
   if (vec.size() <= TSRM_UNSHUFFLE_RSRC_ID(id)) {
     vec.resize(TSRM_UNSHUFFLE_RSRC_ID(id) + 1);
   }
   tsrm_resource_type & type = HPHP::resource_types_table.at(TSRM_UNSHUFFLE_RSRC_ID(id));
   if (vec[TSRM_UNSHUFFLE_RSRC_ID(id)] == nullptr) {
     vec[TSRM_UNSHUFFLE_RSRC_ID(id)] = malloc(type.size);
     if (type.ctor) {
       type.ctor(vec[TSRM_UNSHUFFLE_RSRC_ID(id)] TSRMLS_CC);
     }
   }
   return vec[TSRM_UNSHUFFLE_RSRC_ID(id)];
 }