Exemple #1
0
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  TValue *obj;
  Table *mt;
  lua_lock(L);
  api_checknelems(L, 1);
  obj = index2addr(L, objindex);
  if (ttisnil(L->top - 1))
    mt = NULL;
  else {
    api_check(L, ttistable(L->top - 1), "table expected");
    mt = hvalue(L->top - 1);
  }
  switch (ttypenv(obj)) {
    case LUA_TTABLE: {
      hvalue(obj)->metatable = mt;
      if (mt) {
        luaC_objbarrierback(L, gcvalue(obj), mt);
        luaC_checkfinalizer(L, gcvalue(obj), mt);
      }
      break;
    }
    case LUA_TUSERDATA: {
      uvalue(obj)->metatable = mt;
      if (mt) {
        luaC_objbarrier(L, rawuvalue(obj), mt);
        luaC_checkfinalizer(L, gcvalue(obj), mt);
      }
      break;
    }
    default: {
      G(L)->mt[ttypenv(obj)] = mt;
      break;
    }
  }
  L->top--;
  lua_unlock(L);
  return 1;
}
Exemple #2
0
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  StkId o1 = index2addr(L, index1);
  StkId o2 = index2addr(L, index2);
  return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
}
Exemple #3
0
LUA_API int lua_isuserdata (lua_State *L, int idx) {
  const TValue *o = index2addr(L, idx);
  return (ttisuserdata(o) || ttislightuserdata(o));
}
Exemple #4
0
LUA_API int lua_isnumber (lua_State *L, int idx) {
  TValue n;
  const TValue *o = index2addr(L, idx);
  return tonumber(o, &n);
}
Exemple #5
0
LUA_API int lua_iscfunction (lua_State *L, int idx) {
  StkId o = index2addr(L, idx);
  return (ttislcf(o) || (ttisCclosure(o)));
}
Exemple #6
0
LUA_API int lua_type (lua_State *L, int idx) {
  StkId o = index2addr(L, idx);
  return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
}
Exemple #7
0
LUA_API void lua_pushvalue (lua_State *L, int idx) {
  lua_lock(L);
  setobj2s(L, L->top, index2addr(L, idx));
  api_incr_top(L);
  lua_unlock(L);
}
Exemple #8
0
Fichier : Lapi.c Projet : mniip/LUA
LUA_API void LUA_pushvalue (LUA_State *L, int idx) {
  LUA_lock(L);
  setobj2s(L, L->top, index2addr(L, idx));
  api_incr_top(L);
  LUA_unlock(L);
}
Exemple #9
0
LUA_API int lua_isstring (lua_State *L, int idx) {
  const TValue *o = index2addr(L, idx);
  return (ttisstring(o) || cvt2str(o));
}
Exemple #10
0
LUA_API int lua_isinteger (lua_State *L, int idx) {
  StkId o = index2addr(L, idx);
  return ttisinteger(o);
}
Exemple #11
0
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  lua_lock(L);  /* unlock done in 'auxsetstr' */
  auxsetstr(L, index2addr(L, idx), k);
}
Exemple #12
0
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  lua_lock(L);
  return auxgetstr(L, index2addr(L, idx), k);
}
Exemple #13
0
LUA_API LClosure *lua_tolclosure (lua_State *L, int idx) {
  StkId o = index2addr(L, idx);
  return (!ttisLclosure(o)) ? NULL : (LClosure *) clLvalue(o);
}
Exemple #14
0
/* 从内存池中分配指定大小的内存 
* pMem: 内存池 指针
* sMemorySize: 要分配的内存大小
* 成功时返回分配的内存起始地址,失败返回NULL
************************************************************************/
void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem)
{
#ifdef THREAD
	LOCK(hMutex);
#endif
	sMemorySize = check_align_size(sMemorySize);
	size_t index = 0;
	memory_chunk* tmp = pMem->pfree_mem_chunk;
	for (index = 0; index < pMem->free_mem_chunk_count; index++)
	{
		if (tmp->pfree_mem_addr->count * MINUNITSIZE >= sMemorySize)
		{			
			break;
		}
		
		tmp = tmp->next;
	}
	
	if (index == pMem->free_mem_chunk_count)
	{
#ifdef THREAD
	UNLOCK(hMutex);
#endif
		return NULL;
	}
	pMem->mem_used_size += sMemorySize;
	if (tmp->pfree_mem_addr->count * MINUNITSIZE == sMemorySize)
	{
		// 当要分配的内存大小与当前chunk中的内存大小相同时,从pfree_mem_chunk链表中删除此chunk
		size_t current_index = (tmp->pfree_mem_addr - pMem->pmem_map);
		delete_chunk(pMem->pfree_mem_chunk, tmp);
		tmp->pfree_mem_addr->pmem_chunk = NULL;
		
		push_front(pMem->pfree_mem_chunk_pool, tmp);
		pMem->free_mem_chunk_count--;
		pMem->mem_map_pool_count++;
#ifdef THREAD
		UNLOCK(hMutex);
#endif
		return index2addr(pMem, current_index);
	}
	else
	{
		// 当要分配的内存小于当前chunk中的内存时,更改pfree_mem_chunk中相应chunk的pfree_mem_addr
		
		// 复制当前mem_map_unit
		memory_block copy;
		copy.count = tmp->pfree_mem_addr->count;
		copy.pmem_chunk = tmp;
		// 记录该block的起始和结束索引
		memory_block* current_block = tmp->pfree_mem_addr;
		current_block->count = sMemorySize / MINUNITSIZE;
		size_t current_index = (current_block - pMem->pmem_map);
		pMem->pmem_map[current_index+current_block->count-1].start = current_index;
		current_block->pmem_chunk = NULL; // NULL表示当前内存块已被分配
		// 当前block被一分为二,更新第二个block中的内容
		pMem->pmem_map[current_index+current_block->count].count = copy.count - current_block->count;
		pMem->pmem_map[current_index+current_block->count].pmem_chunk = copy.pmem_chunk;
		// 更新原来的pfree_mem_addr
		tmp->pfree_mem_addr = &(pMem->pmem_map[current_index+current_block->count]);
	
		size_t end_index = current_index + copy.count - 1;
		pMem->pmem_map[end_index].start = current_index + current_block->count;
#ifdef THREAD
		UNLOCK(hMutex);
#endif
		return index2addr(pMem, current_index);
	}	
}
Exemple #15
0
LUA_API int lua_toboolean (lua_State *L, int idx) {
  const TValue *o = index2addr(L, idx);
  return !l_isfalse(o);
}
Exemple #16
0
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  StkId o = index2addr(L, idx);
  return (!ttisthread(o)) ? NULL : thvalue(o);
}
Exemple #17
0
struct connman_device *connman_device_create_from_index(int index)
{
	enum connman_device_type type;
	struct connman_device *device;
	char *devname, *ident = NULL;
	char *addr = NULL, *name = NULL;

	if (index < 0)
		return NULL;

	devname = connman_inet_ifname(index);
	if (devname == NULL)
		return NULL;

	if (__connman_device_isfiltered(devname) == TRUE) {
		connman_info("Ignoring interface %s (filtered)", devname);
		g_free(devname);
		return NULL;
	}

	type = __connman_rtnl_get_device_type(index);

	switch (type) {
	case CONNMAN_DEVICE_TYPE_UNKNOWN:
		connman_info("Ignoring interface %s (type unknown)", devname);
		g_free(devname);
		return NULL;
	case CONNMAN_DEVICE_TYPE_ETHERNET:
	case CONNMAN_DEVICE_TYPE_GADGET:
	case CONNMAN_DEVICE_TYPE_WIFI:
	case CONNMAN_DEVICE_TYPE_CELLULAR:
	case CONNMAN_DEVICE_TYPE_QMI:
	case CONNMAN_DEVICE_TYPE_MK3:
		name = index2ident(index, "");
		addr = index2addr(index);
		break;
	case CONNMAN_DEVICE_TYPE_BLUETOOTH:

	case CONNMAN_DEVICE_TYPE_GPS:
	case CONNMAN_DEVICE_TYPE_VENDOR:
		name = g_strdup(devname);
		break;
	}

	device = connman_device_create(name, type);
	if (device == NULL)
		goto done;

	switch (type) {
	case CONNMAN_DEVICE_TYPE_UNKNOWN:
	case CONNMAN_DEVICE_TYPE_VENDOR:
	case CONNMAN_DEVICE_TYPE_GPS:
		break;
	case CONNMAN_DEVICE_TYPE_ETHERNET:
	case CONNMAN_DEVICE_TYPE_GADGET:
		ident = index2ident(index, NULL);
		break;
	case CONNMAN_DEVICE_TYPE_WIFI:
		ident = index2ident(index, NULL);
		break;
	case CONNMAN_DEVICE_TYPE_BLUETOOTH:
		break;
	case CONNMAN_DEVICE_TYPE_CELLULAR:
		ident = index2ident(index, NULL);
		break;
	case CONNMAN_DEVICE_TYPE_QMI:
	case CONNMAN_DEVICE_TYPE_MK3:
		ident = index2ident(index, NULL);
		break;
	}

	connman_device_set_index(device, index);
	connman_device_set_interface(device, devname);

	if (ident != NULL) {
		connman_device_set_ident(device, ident);
		g_free(ident);
	}

	connman_device_set_string(device, "Address", addr);

done:
	g_free(devname);
	g_free(name);
	g_free(addr);

	return device;
}