diff options
author | Mike Pall <mike> | 2011-01-12 21:34:11 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2011-01-12 21:34:11 +0100 |
commit | e7b08b2361dd9e3f945dfd4b7e34c4aad2243582 (patch) | |
tree | 3b62d5f25f8abe01c524f154a820c974efa48eca /src | |
parent | a5e48285e39af2e794288091fafc02e113e1fa82 (diff) | |
download | luajit-e7b08b2361dd9e3f945dfd4b7e34c4aad2243582.tar.gz luajit-e7b08b2361dd9e3f945dfd4b7e34c4aad2243582.tar.bz2 luajit-e7b08b2361dd9e3f945dfd4b7e34c4aad2243582.zip |
FFI: Simplify logic for pointer arithmetic.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib_ffi.c | 18 |
1 files changed, 5 insertions, 13 deletions
diff --git a/src/lib_ffi.c b/src/lib_ffi.c index b040a652..aeeb0bc1 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c | |||
@@ -94,15 +94,13 @@ static void ffi_checkarith(lua_State *L, CTState *cts, FFIArith *fa) | |||
94 | for (i = 0; i < 2; i++, o++) { | 94 | for (i = 0; i < 2; i++, o++) { |
95 | if (tviscdata(o)) { | 95 | if (tviscdata(o)) { |
96 | GCcdata *cd = cdataV(o); | 96 | GCcdata *cd = cdataV(o); |
97 | CTypeID id = (CTypeID)cd->typeid; | 97 | CType *ct = ctype_raw(cts, (CTypeID)cd->typeid); |
98 | CType *ct = ctype_get(cts, id); | ||
99 | uint8_t *p = (uint8_t *)cdataptr(cd); | 98 | uint8_t *p = (uint8_t *)cdataptr(cd); |
100 | if (ctype_isref(ct->info)) { | 99 | if (ctype_isptr(ct->info)) { |
101 | lua_assert(ct->size == CTSIZE_PTR); | 100 | p = (uint8_t *)cdata_getptr(p, ct->size); |
102 | p = *(uint8_t **)p; | 101 | if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); |
103 | id = ctype_cid(ct->info); | ||
104 | } | 102 | } |
105 | fa->ct[i] = ctype_raw(cts, id); | 103 | fa->ct[i] = ct; |
106 | fa->p[i] = p; | 104 | fa->p[i] = p; |
107 | } else if (tvisnum(o)) { | 105 | } else if (tvisnum(o)) { |
108 | fa->ct[i] = ctype_get(cts, CTID_DOUBLE); | 106 | fa->ct[i] = ctype_get(cts, CTID_DOUBLE); |
@@ -134,10 +132,6 @@ static int ffi_arith_ptr(lua_State *L, CTState *cts, FFIArith *fa, MMS mm) | |||
134 | sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ | 132 | sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ |
135 | if (sz == 0 || sz == CTSIZE_INVALID) | 133 | if (sz == 0 || sz == CTSIZE_INVALID) |
136 | lj_err_caller(L, LJ_ERR_FFI_INVSIZE); | 134 | lj_err_caller(L, LJ_ERR_FFI_INVSIZE); |
137 | if (ctype_isptr(ctp->info)) | ||
138 | pp = (uint8_t *)cdata_getptr(pp, ctp->size); | ||
139 | if (ctype_isptr(fa->ct[1]->info)) | ||
140 | fa->p[1] = (uint8_t *)cdata_getptr(fa->p[1], fa->ct[1]->size); | ||
141 | diff = ((intptr_t)pp - (intptr_t)fa->p[1]) / (int32_t)sz; | 135 | diff = ((intptr_t)pp - (intptr_t)fa->p[1]) / (int32_t)sz; |
142 | /* All valid pointer differences on x64 are in (-2^47, +2^47), | 136 | /* All valid pointer differences on x64 are in (-2^47, +2^47), |
143 | ** which fits into a double without loss of precision. | 137 | ** which fits into a double without loss of precision. |
@@ -162,8 +156,6 @@ static int ffi_arith_ptr(lua_State *L, CTState *cts, FFIArith *fa, MMS mm) | |||
162 | sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ | 156 | sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ |
163 | if (sz == CTSIZE_INVALID) | 157 | if (sz == CTSIZE_INVALID) |
164 | lj_err_caller(L, LJ_ERR_FFI_INVSIZE); | 158 | lj_err_caller(L, LJ_ERR_FFI_INVSIZE); |
165 | if (ctype_isptr(ctp->info)) | ||
166 | pp = (uint8_t *)cdata_getptr(pp, ctp->size); | ||
167 | pp += idx*(int32_t)sz; /* Compute pointer + index. */ | 159 | pp += idx*(int32_t)sz; /* Compute pointer + index. */ |
168 | id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), | 160 | id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), |
169 | CTSIZE_PTR); | 161 | CTSIZE_PTR); |