Permalink
Browse files

Change the DB/DB_ENV userdata to structs

  • Loading branch information...
1 parent 45dd7f2 commit fe3427ef2dd50daabc58817f74c943a17861b72b @hoelzro committed Jul 22, 2010
Showing with 29 additions and 20 deletions.
  1. +15 −10 db_ops.c
  2. +14 −10 env_ops.c
View
@@ -27,24 +27,29 @@
#include <lualib.h>
#include <string.h>
+struct DB_wrapper {
+ DB *db;
+};
+
DB *luabdb_todb(lua_State *L, int narg)
{
- DB **dbp = (DB **) luaL_checkudata(L, narg, LUABDB_DB);
- if(! *dbp) {
+ struct DB_wrapper *wrap = luaL_checkudata(L, narg, LUABDB_DB);
+
+ if(! wrap->db) {
luaL_error(L, "Attempt to use a closed DB handle (%p)", lua_topointer(L, narg));
}
- return *dbp;
+ return wrap->db;
}
DB **luabdb_createdbp(lua_State *L)
{
- DB **dbp;
+ struct DB_wrapper *wrap;
- dbp = lua_newuserdata(L, sizeof(DB *));
+ wrap = lua_newuserdata(L, sizeof(struct DB_wrapper));
luaL_getmetatable(L, LUABDB_DB);
lua_setmetatable(L, -2);
- return dbp;
+ return &(wrap->db);
}
static int db_op_put(lua_State *L)
@@ -105,13 +110,13 @@ static int db_op_get(lua_State *L)
static int db_op_close(lua_State *L)
{
- DB **dbp;
+ struct DB_wrapper *wrap;
int flags = 0;
luabdb_todb(L, 1); /* for error checking */
- dbp = (DB **) luaL_checkudata(L, 1, LUABDB_DB);
- (*dbp)->close(*dbp, flags);
- *dbp = NULL;
+ wrap = (struct DB_wrapper *) luaL_checkudata(L, 1, LUABDB_DB);
+ wrap->db->close(wrap->db, flags);
+ wrap->db = NULL;
return 0;
}
View
@@ -26,35 +26,39 @@
#include <lauxlib.h>
#include <lualib.h>
+struct DB_ENV_wrapper {
+ DB_ENV *env;
+};
+
DB_ENV *luabdb_toenv(lua_State *L, int narg)
{
- DB_ENV **envp = (DB_ENV **) luaL_checkudata(L, narg, LUABDB_ENV);
- if(! *envp) {
+ struct DB_ENV_wrapper *wrapper = (struct DB_ENV_wrapper *) luaL_checkudata(L, narg, LUABDB_ENV);
+ if(! wrapper->env) {
luaL_error(L, "Attempt to use a closed DB_ENV handle (%p)", lua_topointer(L, narg));
}
- return *envp;
+ return wrapper->env;
}
DB_ENV **luabdb_createenvp(lua_State *L)
{
- DB_ENV **envp;
+ struct DB_ENV_wrapper *wrapper;
- envp = lua_newuserdata(L, sizeof(DB_ENV *));
+ wrapper = lua_newuserdata(L, sizeof(struct DB_ENV_wrapper));
luaL_getmetatable(L, LUABDB_ENV);
lua_setmetatable(L, -2);
- return envp;
+ return &(wrapper->env);
}
static int env_op_close(lua_State *L)
{
- DB_ENV **envp;
+ struct DB_ENV_wrapper *wrapper;
u_int32_t flags = 0;
luabdb_toenv(L, 1); /* for error checking */
- envp = (DB_ENV **) luaL_checkudata(L, 1, LUABDB_ENV);
- (*envp)->close(*envp, flags);
- *envp = NULL;
+ wrapper = (struct DB_ENV_wrapper *) luaL_checkudata(L, 1, LUABDB_ENV);
+ wrapper->env->close(wrapper->env, flags);
+ wrapper->env = NULL;
return 0;
}

0 comments on commit fe3427e

Please sign in to comment.