| 1 | /* ====================================================================== |
| 2 | ** memarray.c - Copyright (C) 2005-2006 Varol Kaptan |
| 3 | ** see LICENSE for more information |
| 4 | ** ====================================================================== |
| 5 | ** vim: set ts=3 et: */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #if defined(MEMARRAY_USE_OPENGL) |
| 12 | #if defined(__APPLE__) && defined(__MACH__) |
| 13 | #include <OpenGL/gl.h> |
| 14 | #include <OpenGL/glext.h> |
| 15 | #else |
| 16 | #include <GL/gl.h> |
| 17 | #include <GL/glext.h> |
| 18 | #endif |
| 19 | #endif |
| 20 | |
| 21 | #include <lua.h> |
| 22 | #include <lauxlib.h> |
| 23 | |
| 24 | #define MYNAME "memarray" |
| 25 | #define MYTYPE "memarray*" |
| 26 | #define VERSION "0.5" |
| 27 | |
| 28 | #define MEMARRAY_TCHAR 0 |
| 29 | #define MEMARRAY_TUCHAR 1 |
| 30 | #define MEMARRAY_TSHORT 2 |
| 31 | #define MEMARRAY_TUSHORT 3 |
| 32 | #define MEMARRAY_TINT 4 |
| 33 | #define MEMARRAY_TUINT 5 |
| 34 | #define MEMARRAY_TLONG 6 |
| 35 | #define MEMARRAY_TULONG 7 |
| 36 | #define MEMARRAY_TFLOAT 8 |
| 37 | #define MEMARRAY_TDOUBLE 9 |
| 38 | #if !defined(MEMARRAY_USE_OPENGL) |
| 39 | #define MEMARRAY_NUM_TYPES 10 |
| 40 | #else |
| 41 | #define MEMARRAY_TGLBOOLEAN 10 |
| 42 | #define MEMARRAY_TGLBYTE 11 |
| 43 | #define MEMARRAY_TGLUBYTE 12 |
| 44 | #define MEMARRAY_TGLSHORT 13 |
| 45 | #define MEMARRAY_TGLUSHORT 14 |
| 46 | #define MEMARRAY_TGLINT 15 |
| 47 | #define MEMARRAY_TGLUINT 16 |
| 48 | #define MEMARRAY_TGLSIZEI 17 |
| 49 | #define MEMARRAY_TGLENUM 18 |
| 50 | #define MEMARRAY_TGLBITFIELD 19 |
| 51 | #define MEMARRAY_TGLFLOAT 20 |
| 52 | #define MEMARRAY_TGLDOUBLE 21 |
| 53 | #define MEMARRAY_NUM_TYPES 22 |
| 54 | #endif |
| 55 | |
| 56 | #define MEMARRAY_TYPE_MASK 0x1F |
| 57 | #define MEMARRAY_INHERITED 0x20 |
| 58 | |
| 59 | #define mem_type(m) ((m)->flags & MEMARRAY_TYPE_MASK) |
| 60 | #define mem_inherited(m) ((m)->flags & MEMARRAY_INHERITED) |
| 61 | |
| 62 | typedef struct { |
| 63 | char *name; |
| 64 | int size; |
| 65 | } typeinfo; |
| 66 | |
| 67 | const typeinfo m_types[MEMARRAY_NUM_TYPES] = { |
| 68 | { "char", sizeof(char) }, |
| 69 | { "uchar", sizeof(unsigned char) }, |
| 70 | { "short", sizeof(short) }, |
| 71 | { "ushort", sizeof(unsigned short) }, |
| 72 | { "int", sizeof(int) }, |
| 73 | { "uint", sizeof(unsigned int) }, |
| 74 | { "long", sizeof(long) }, |
| 75 | { "ulong", sizeof(unsigned long) }, |
| 76 | { "float", sizeof(float) }, |
| 77 | { "double", sizeof(double) }, |
| 78 | #if defined(MEMARRAY_USE_OPENGL) |
| 79 | { "GLboolean", sizeof(GLboolean) }, |
| 80 | { "GLbyte", sizeof(GLbyte) }, |
| 81 | { "GLubyte", sizeof(GLubyte) }, |
| 82 | { "GLshort", sizeof(GLshort) }, |
| 83 | { "GLushort", sizeof(GLushort) }, |
| 84 | { "GLint", sizeof(GLint) }, |
| 85 | { "GLuint", sizeof(GLuint) }, |
| 86 | { "GLsizei", sizeof(GLsizei) }, |
| 87 | { "GLenum", sizeof(GLenum) }, |
| 88 | { "GLbitfield",sizeof(GLbitfield) }, |
| 89 | { "GLfloat", sizeof(GLfloat) }, |
| 90 | { "GLdouble", sizeof(GLdouble) }, |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | typedef struct memarray { |
| 95 | size_t size; |
| 96 | size_t length; |
| 97 | unsigned int flags; |
| 98 | void *data; |
| 99 | } memarray_t; |
| 100 | |
| 101 | /* we keep discovered endianness here (1 = little endian, 0 = big endian) */ |
| 102 | static int little_endian = 0; |
| 103 | |
| 104 | static int get_memtype(const char * typename) |
| 105 | { |
| 106 | int type; |
| 107 | |
| 108 | for (type = 0; type < MEMARRAY_NUM_TYPES; ++type) { |
| 109 | if (strcmp(typename, m_types[type].name) == 0) |
| 110 | return type; |
| 111 | } |
| 112 | |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | static const void * check_lightuserdata(lua_State *L, int n) |
| 117 | { |
| 118 | if (lua_type(L,n) != LUA_TLIGHTUSERDATA) |
| 119 | luaL_typerror(L, n, "lightuserdata"); |
| 120 | return (const void *) lua_touserdata(L, n); |
| 121 | } |
| 122 | |
| 123 | static memarray_t *memarray_get(lua_State * L, int i) |
| 124 | { |
| 125 | if (luaL_checkudata(L, i, MYTYPE) == NULL) |
| 126 | luaL_typerror(L, i, MYTYPE); |
| 127 | return lua_touserdata(L, i); |
| 128 | } |
| 129 | |
| 130 | static memarray_t *memarray_new(lua_State * L) |
| 131 | { |
| 132 | memarray_t *m = lua_newuserdata(L, sizeof(memarray_t)); |
| 133 | |
| 134 | m->data = NULL; |
| 135 | m->size = 0; |
| 136 | m->length = 0; |
| 137 | m->flags = MEMARRAY_INHERITED; |
| 138 | |
| 139 | luaL_getmetatable(L, MYTYPE); |
| 140 | lua_setmetatable(L, -2); |
| 141 | /* fprintf(stderr, "memarray_new @ %p\n", (void *)m); */ |
| 142 | return m; |
| 143 | } |
| 144 | |
| 145 | static void memarray_delete(memarray_t *m) |
| 146 | { |
| 147 | if (!mem_inherited(m)) |
| 148 | free(m->data); |
| 149 | |
| 150 | m->data = NULL; |
| 151 | m->size = 0; |
| 152 | m->length = 0; |
| 153 | m->flags = MEMARRAY_INHERITED; |
| 154 | } |
| 155 | |
| 156 | /* self, 'type', n, [address] */ |
| 157 | static int Lrealloc(lua_State * L) |
| 158 | { |
| 159 | memarray_t *m; |
| 160 | const char *typename; |
| 161 | int type; |
| 162 | size_t length; |
| 163 | size_t size; |
| 164 | void * data; |
| 165 | char errmsg[128]; |
| 166 | |
| 167 | m = memarray_get(L, 1); |
| 168 | typename = luaL_optstring(L, 2, "uchar"); |
| 169 | |
| 170 | if ( (type = get_memtype(typename)) < 0 ) { |
| 171 | luaL_error(L, "memarray:realloc(): unknown type '%s'", typename); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | length = (size_t) luaL_optnumber(L, 3, 0); |
| 176 | size = length * m_types[type].size; |
| 177 | |
| 178 | if (lua_gettop(L) > 3) { |
| 179 | /* do not allocate memory, inherit something else */ |
| 180 | memarray_delete(m); |
| 181 | m->size = size; |
| 182 | m->length = length; |
| 183 | m->flags = type | MEMARRAY_INHERITED; |
| 184 | m->data = lua_touserdata(L, 4); |
| 185 | } else { |
| 186 | /* allocate memory */ |
| 187 | memarray_delete(m); |
| 188 | data = (void *) malloc(size); |
| 189 | if ((size != 0) && (data == NULL)) { |
| 190 | m->flags = type | MEMARRAY_INHERITED; |
| 191 | sprintf(errmsg, "memarray:__call(): memory allocation failure " |
| 192 | "for %zd bytes (%s x %zd)\n", size, typename, length); |
| 193 | luaL_error(L, errmsg); |
| 194 | } else { |
| 195 | m->flags = type; |
| 196 | m->size = size; |
| 197 | m->length = length; |
| 198 | m->data = data; |
| 199 | } |
| 200 | } |
| 201 | lua_settop(L, 1); |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | /* memarray, 'type', n, [address] */ |
| 206 | static int L__call(lua_State * L) |
| 207 | { |
| 208 | memarray_t *m; |
| 209 | m = memarray_new(L); |
| 210 | lua_replace(L, 1); |
| 211 | return Lrealloc(L); |
| 212 | } |
| 213 | |
| 214 | /* 'type' */ |
| 215 | static int Lsizeof(lua_State * L) |
| 216 | { |
| 217 | const char *typename; |
| 218 | int type; |
| 219 | |
| 220 | typename = luaL_checkstring(L, 1); |
| 221 | |
| 222 | if ( (type = get_memtype(typename)) < 0 ) { |
| 223 | luaL_error(L, "memarray:sizeof(): unknown type '%s'", typename); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | lua_pushnumber(L, m_types[type].size); |
| 228 | |
| 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | /* dst, src, size(in bytes) */ |
| 233 | static int Lmemcpy(lua_State * L) |
| 234 | { |
| 235 | void * dst = (void *) check_lightuserdata(L, 1); |
| 236 | const void * src = check_lightuserdata(L, 2); |
| 237 | size_t size = (size_t) luaL_checknumber(L, 3); |
| 238 | memcpy(dst, src, size); |
| 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | /* self, index */ |
| 243 | static int L__index(lua_State * L) |
| 244 | { |
| 245 | memarray_t *m; |
| 246 | size_t index; |
| 247 | |
| 248 | m = memarray_get(L, 1); |
| 249 | |
| 250 | switch (lua_type(L, 2)) { |
| 251 | case LUA_TNUMBER: |
| 252 | index = (size_t) lua_tonumber(L, 2); |
| 253 | if ( /*(index < 0) ||*/ (index >= m->length) ) { |
| 254 | lua_pushnil(L); |
| 255 | luaL_error(L, "index out of range"); |
| 256 | } else switch (mem_type(m)) { |
| 257 | case MEMARRAY_TCHAR: lua_pushnumber(L, ((char *)(m->data))[index]); break; |
| 258 | case MEMARRAY_TUCHAR: lua_pushnumber(L, ((unsigned char *)(m->data))[index]); break; |
| 259 | case MEMARRAY_TSHORT: lua_pushnumber(L, ((short *)(m->data))[index]); break; |
| 260 | case MEMARRAY_TUSHORT: lua_pushnumber(L, ((unsigned short *)(m->data))[index]); break; |
| 261 | case MEMARRAY_TINT: lua_pushnumber(L, ((int *)(m->data))[index]); break; |
| 262 | case MEMARRAY_TUINT: lua_pushnumber(L, ((unsigned int *)(m->data))[index]); break; |
| 263 | case MEMARRAY_TLONG: lua_pushnumber(L, ((long *)(m->data))[index]); break; |
| 264 | case MEMARRAY_TULONG: lua_pushnumber(L, ((unsigned long *)(m->data))[index]); break; |
| 265 | case MEMARRAY_TFLOAT: lua_pushnumber(L, ((float *)(m->data))[index]); break; |
| 266 | case MEMARRAY_TDOUBLE: lua_pushnumber(L, ((double *)(m->data))[index]); break; |
| 267 | #if defined(MEMARRAY_USE_OPENGL) |
| 268 | case MEMARRAY_TGLBOOLEAN: lua_pushnumber(L, ((GLboolean *)(m->data))[index]); break; |
| 269 | case MEMARRAY_TGLBYTE: lua_pushnumber(L, ((GLbyte *)(m->data))[index]); break; |
| 270 | case MEMARRAY_TGLUBYTE: lua_pushnumber(L, ((GLubyte *)(m->data))[index]); break; |
| 271 | case MEMARRAY_TGLSHORT: lua_pushnumber(L, ((GLshort *)(m->data))[index]); break; |
| 272 | case MEMARRAY_TGLUSHORT: lua_pushnumber(L, ((GLushort *)(m->data))[index]); break; |
| 273 | case MEMARRAY_TGLINT: lua_pushnumber(L, ((GLint *)(m->data))[index]); break; |
| 274 | case MEMARRAY_TGLUINT: lua_pushnumber(L, ((GLuint *)(m->data))[index]); break; |
| 275 | case MEMARRAY_TGLSIZEI: lua_pushnumber(L, ((GLsizei *)(m->data))[index]); break; |
| 276 | case MEMARRAY_TGLENUM: lua_pushnumber(L, ((GLenum *)(m->data))[index]); break; |
| 277 | case MEMARRAY_TGLBITFIELD: lua_pushnumber(L, ((GLbitfield*)(m->data))[index]); break; |
| 278 | case MEMARRAY_TGLFLOAT: lua_pushnumber(L, ((GLfloat *)(m->data))[index]); break; |
| 279 | case MEMARRAY_TGLDOUBLE: lua_pushnumber(L, ((GLdouble *)(m->data))[index]); break; |
| 280 | #endif |
| 281 | default: lua_pushnil(L); break; |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | default: |
| 286 | lua_getmetatable(L, 1); |
| 287 | lua_replace(L, 1); |
| 288 | lua_rawget(L, 1); |
| 289 | break; |
| 290 | } |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | /* self, index, value */ |
| 295 | static int L__newindex(lua_State * L) |
| 296 | { |
| 297 | memarray_t *m; |
| 298 | size_t index; |
| 299 | lua_Number value; |
| 300 | |
| 301 | m = memarray_get(L, 1); |
| 302 | |
| 303 | switch (lua_type(L, 2)) { |
| 304 | case LUA_TNUMBER: |
| 305 | index = (size_t) lua_tonumber(L, 2); |
| 306 | value = lua_tonumber(L, 3); |
| 307 | if ( /*(index < 0) ||*/ (index >= m->length) ) { |
| 308 | luaL_error(L, "index out of range"); |
| 309 | } else switch (mem_type(m)) { |
| 310 | case MEMARRAY_TCHAR: ((char *)m->data)[index] = (char) value; break; |
| 311 | case MEMARRAY_TUCHAR: ((unsigned char *)m->data)[index] = (unsigned char) value; break; |
| 312 | case MEMARRAY_TSHORT: ((short *)m->data)[index] = (short) value; break; |
| 313 | case MEMARRAY_TUSHORT: ((unsigned short *)m->data)[index] = (unsigned short) value; break; |
| 314 | case MEMARRAY_TINT: ((int *)m->data)[index] = (int) value; break; |
| 315 | case MEMARRAY_TUINT: ((unsigned int *)m->data)[index] = (unsigned int) value; break; |
| 316 | case MEMARRAY_TLONG: ((long *)m->data)[index] = (long) value; break; |
| 317 | case MEMARRAY_TULONG: ((unsigned long *)m->data)[index] = (unsigned long) value; break; |
| 318 | case MEMARRAY_TFLOAT: ((float *)m->data)[index] = (float) value; break; |
| 319 | case MEMARRAY_TDOUBLE: ((double *)m->data)[index] = (double) value; break; |
| 320 | #if defined(MEMARRAY_USE_OPENGL) |
| 321 | case MEMARRAY_TGLBOOLEAN: ((GLboolean *)(m->data))[index] = (GLboolean ) value; break; |
| 322 | case MEMARRAY_TGLBYTE: ((GLbyte *)(m->data))[index] = (GLbyte ) value; break; |
| 323 | case MEMARRAY_TGLUBYTE: ((GLubyte *)(m->data))[index] = (GLubyte ) value; break; |
| 324 | case MEMARRAY_TGLSHORT: ((GLshort *)(m->data))[index] = (GLshort ) value; break; |
| 325 | case MEMARRAY_TGLUSHORT: ((GLushort *)(m->data))[index] = (GLushort ) value; break; |
| 326 | case MEMARRAY_TGLINT: ((GLint *)(m->data))[index] = (GLint ) value; break; |
| 327 | case MEMARRAY_TGLUINT: ((GLuint *)(m->data))[index] = (GLuint ) value; break; |
| 328 | case MEMARRAY_TGLSIZEI: ((GLsizei *)(m->data))[index] = (GLsizei ) value; break; |
| 329 | case MEMARRAY_TGLENUM: ((GLenum *)(m->data))[index] = (GLenum ) value; break; |
| 330 | case MEMARRAY_TGLBITFIELD: ((GLbitfield*)(m->data))[index] = (GLbitfield) value; break; |
| 331 | case MEMARRAY_TGLFLOAT: ((GLfloat *)(m->data))[index] = (GLfloat ) value; break; |
| 332 | case MEMARRAY_TGLDOUBLE: ((GLdouble *)(m->data))[index] = (GLdouble ) value; break; |
| 333 | #endif |
| 334 | default: lua_pushnil(L); break; |
| 335 | } |
| 336 | break; |
| 337 | |
| 338 | default: |
| 339 | luaL_error(L, "memarray*:__newindex key must be number"); |
| 340 | break; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* self */ |
| 346 | static int L__tostring(lua_State * L) |
| 347 | { |
| 348 | memarray_t *m = memarray_get(L, 1); |
| 349 | char s[256]; |
| 350 | |
| 351 | sprintf(s, |
| 352 | mem_inherited(m) ? "memarray(%s,%zd,%p,inherited)@%p": "memarray(%s,%zd,%p)@%p" , |
| 353 | m_types[mem_type(m)].name, m->length, (void *)m->data, (void *)m); |
| 354 | |
| 355 | lua_pushstring(L, s); |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | /* self */ |
| 360 | static int Lsize(lua_State *L) |
| 361 | { |
| 362 | memarray_t *m = memarray_get(L, 1); |
| 363 | lua_pushnumber(L, m->size); |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | /* self */ |
| 368 | static int Llength(lua_State *L) |
| 369 | { |
| 370 | memarray_t *m = memarray_get(L, 1); |
| 371 | lua_pushnumber(L, m->length); |
| 372 | return 1; |
| 373 | } |
| 374 | |
| 375 | /* self, [index] */ |
| 376 | static int Lptr(lua_State *L) |
| 377 | { |
| 378 | memarray_t *m = memarray_get(L, 1); |
| 379 | size_t index = (size_t) luaL_optnumber(L, 2, 0); |
| 380 | |
| 381 | if ( /*(index < 0) ||*/ (index >= m->length) ) { |
| 382 | luaL_error(L, "index out of range"); |
| 383 | return 0; |
| 384 | } else { |
| 385 | char *ptr = m->data; |
| 386 | lua_pushlightuserdata(L, (void *)(ptr + index * m_types[mem_type(m)].size)); |
| 387 | return 1; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* self */ |
| 392 | static int L__gc(lua_State *L) |
| 393 | { |
| 394 | memarray_t *m = memarray_get(L, 1); |
| 395 | /* |
| 396 | fprintf(stderr, "invoking __gc for "); |
| 397 | fprintf(stderr, |
| 398 | mem_inherited(m) ? "memarray(%s,%zd,%p,inherited)@%p\n": "memarray(%s,%zd,%p)@%p\n" , |
| 399 | m_types[mem_type(m)].name, m->length, (void *)m->data, (void *)m); |
| 400 | */ |
| 401 | memarray_delete(m); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | /* self */ |
| 406 | static int Lto_str(lua_State *L) |
| 407 | { |
| 408 | memarray_t *m = memarray_get(L, 1); |
| 409 | luaL_Buffer b; |
| 410 | |
| 411 | luaL_buffinit(L, &b); |
| 412 | luaL_addlstring(&b, m->data, m->size); |
| 413 | luaL_pushresult(&b); |
| 414 | |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | /* self, str */ |
| 419 | static int Lfrom_str(lua_State *L) |
| 420 | { |
| 421 | size_t len; |
| 422 | memarray_t *m = memarray_get(L, 1); |
| 423 | const char *str = luaL_checklstring(L, 2, &len); |
| 424 | |
| 425 | if (m->size != len) { |
| 426 | luaL_error(L, "memarray:to_str size mismatch"); |
| 427 | return 0; |
| 428 | } else { |
| 429 | memcpy(m->data, str, len); |
| 430 | } |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | #define PUT_SWAP_2(B, src) \ |
| 435 | luaL_putchar((B), (src)[1]); \ |
| 436 | luaL_putchar((B), (src)[0]); |
| 437 | |
| 438 | #define PUT_SWAP_4(B, src) \ |
| 439 | luaL_putchar((B), (src)[3]); \ |
| 440 | luaL_putchar((B), (src)[2]); \ |
| 441 | luaL_putchar((B), (src)[1]); \ |
| 442 | luaL_putchar((B), (src)[0]); |
| 443 | |
| 444 | #define PUT_SWAP_8(B, src) \ |
| 445 | luaL_putchar((B), (src)[7]); \ |
| 446 | luaL_putchar((B), (src)[6]); \ |
| 447 | luaL_putchar((B), (src)[5]); \ |
| 448 | luaL_putchar((B), (src)[4]); \ |
| 449 | luaL_putchar((B), (src)[3]); \ |
| 450 | luaL_putchar((B), (src)[2]); \ |
| 451 | luaL_putchar((B), (src)[1]); \ |
| 452 | luaL_putchar((B), (src)[0]); |
| 453 | |
| 454 | /* self */ |
| 455 | static int Lto_netstr(lua_State *L) |
| 456 | { |
| 457 | memarray_t *m = memarray_get(L, 1); |
| 458 | luaL_Buffer b; |
| 459 | size_t item_size; |
| 460 | unsigned char *src, *end; |
| 461 | |
| 462 | luaL_buffinit(L, &b); |
| 463 | item_size = m->size / m->length; |
| 464 | |
| 465 | if (little_endian && (item_size != 1)) { |
| 466 | src = (unsigned char *) m->data; |
| 467 | end = src + m->size; |
| 468 | switch (item_size) { |
| 469 | case 2: |
| 470 | while (src < end) { PUT_SWAP_2(&b, src); src += 2; } |
| 471 | break; |
| 472 | |
| 473 | case 4: |
| 474 | while (src < end) { PUT_SWAP_4(&b, src); src += 4; } |
| 475 | break; |
| 476 | |
| 477 | case 8: |
| 478 | while (src < end) { PUT_SWAP_8(&b, src); src += 8; } |
| 479 | break; |
| 480 | |
| 481 | default: |
| 482 | luaL_error(L, "memarray:to_netstr(): unsupported item_size"); |
| 483 | break; |
| 484 | } |
| 485 | } else { |
| 486 | luaL_addlstring(&b, m->data, m->size); |
| 487 | } |
| 488 | |
| 489 | luaL_pushresult(&b); |
| 490 | |
| 491 | return 1; |
| 492 | } |
| 493 | |
| 494 | #define COPY_SWAP_2(src,dst) \ |
| 495 | dst[0] = src[1]; \ |
| 496 | dst[1] = src[0]; |
| 497 | |
| 498 | #define COPY_SWAP_4(src, dst) \ |
| 499 | dst[0] = src[3]; \ |
| 500 | dst[1] = src[2]; \ |
| 501 | dst[2] = src[1]; \ |
| 502 | dst[3] = src[0]; |
| 503 | |
| 504 | #define COPY_SWAP_8(src, dst) \ |
| 505 | dst[0] = src[7]; \ |
| 506 | dst[1] = src[6]; \ |
| 507 | dst[2] = src[5]; \ |
| 508 | dst[3] = src[4]; \ |
| 509 | dst[4] = src[3]; \ |
| 510 | dst[5] = src[2]; \ |
| 511 | dst[6] = src[1]; \ |
| 512 | dst[7] = src[0]; |
| 513 | |
| 514 | /* self, str */ |
| 515 | static int Lfrom_netstr(lua_State *L) |
| 516 | { |
| 517 | size_t len; |
| 518 | memarray_t *m = memarray_get(L, 1); |
| 519 | const char *str = luaL_checklstring(L, 2, &len); |
| 520 | size_t item_size; |
| 521 | unsigned char *src, *end, *dst; |
| 522 | |
| 523 | if (m->size != len) { |
| 524 | luaL_error(L, "memarray:to_netstr size mismatch"); |
| 525 | return 0; |
| 526 | } else { |
| 527 | item_size = m->size / m->length; |
| 528 | |
| 529 | if (little_endian && (item_size != 1)) { |
| 530 | src = (unsigned char *) str; |
| 531 | end = src + m->size; |
| 532 | dst = (unsigned char *) m->data; |
| 533 | switch (item_size) { |
| 534 | case 2: |
| 535 | while (src < end) { COPY_SWAP_2(src, dst); src += 2; dst += 2; } |
| 536 | break; |
| 537 | |
| 538 | case 4: |
| 539 | while (src < end) { COPY_SWAP_4(src, dst); src += 4; dst += 4; } |
| 540 | break; |
| 541 | |
| 542 | case 8: |
| 543 | while (src < end) { COPY_SWAP_8(src, dst); src += 8; dst += 8; } |
| 544 | break; |
| 545 | |
| 546 | default: |
| 547 | luaL_error(L, "memarray:from_netstr(): unsupported item_size"); |
| 548 | break; |
| 549 | } |
| 550 | } else { |
| 551 | memcpy(m->data, str, len); |
| 552 | } |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static const luaL_reg reg_memarray[] = |
| 558 | { |
| 559 | { "__call", L__call }, |
| 560 | { "sizeof", Lsizeof }, |
| 561 | { "memcpy", Lmemcpy }, |
| 562 | { NULL, NULL } |
| 563 | }; |
| 564 | |
| 565 | static const luaL_reg reg_memarray_ptr[] = |
| 566 | { |
| 567 | { "__index", L__index }, |
| 568 | { "__newindex", L__newindex }, |
| 569 | { "__tostring", L__tostring }, |
| 570 | { "__gc", L__gc }, |
| 571 | { "size", Lsize }, |
| 572 | { "length", Llength }, |
| 573 | { "ptr", Lptr }, |
| 574 | { "to_str", Lto_str }, |
| 575 | { "from_str", Lfrom_str }, |
| 576 | { "to_netstr", Lto_netstr }, |
| 577 | { "from_netstr", Lfrom_netstr}, |
| 578 | { "realloc", Lrealloc }, |
| 579 | { NULL, NULL } |
| 580 | }; |
| 581 | |
| 582 | LUALIB_API int luaopen_memarray(lua_State * L) |
| 583 | { |
| 584 | /* we discover endianness here */ |
| 585 | int one = 1; |
| 586 | little_endian = (*(char *)&one == 1); |
| 587 | |
| 588 | luaL_newmetatable(L, MYTYPE); |
| 589 | luaL_openlib(L, NULL, reg_memarray_ptr, 0); |
| 590 | |
| 591 | lua_newtable(L); |
| 592 | lua_pushliteral(L, "VERSION"); |
| 593 | lua_pushliteral(L, VERSION); |
| 594 | lua_settable(L, -3); |
| 595 | |
| 596 | lua_pushliteral(L, MYNAME); |
| 597 | lua_pushvalue(L, -2); |
| 598 | lua_settable(L, LUA_GLOBALSINDEX); |
| 599 | |
| 600 | lua_pushvalue(L, -1); |
| 601 | luaL_openlib(L, NULL, reg_memarray, 0); |
| 602 | |
| 603 | lua_setmetatable(L, -2); |
| 604 | return 1; |
| 605 | } |