about summary refs log tree commit diff stats
path: root/src/box32.c
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-08-28 17:12:00 +0200
committerptitSeb <sebastien.chev@gmail.com>2024-08-28 17:12:00 +0200
commitb7be225846153a1ed44b149b6600fb86c3fa0b42 (patch)
tree5d8ccd52428f0f1e788dd80c612826d89d7a10a5 /src/box32.c
parent041eb4e69d833a4bf5f68df59c1fe20385b5ad9f (diff)
downloadbox64-b7be225846153a1ed44b149b6600fb86c3fa0b42.tar.gz
box64-b7be225846153a1ed44b149b6600fb86c3fa0b42.zip
[BOX32] More 32bits wrapped functions
Diffstat (limited to 'src/box32.c')
-rw-r--r--src/box32.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/box32.c b/src/box32.c
index 85a647a6..24af6b06 100644
--- a/src/box32.c
+++ b/src/box32.c
@@ -11,6 +11,7 @@
 
 KHASH_MAP_INIT_INT64(to, ulong_t);
 KHASH_MAP_INIT_INT(from, uintptr_t);
+KHASH_MAP_INIT_STR(strings, char*);
 
 static kh_from_t*   hash_from;
 static kh_to_t*     hash_to;
@@ -22,6 +23,7 @@ static int          hash_running = 0;
 // locale
 static kh_from_t*   locale_from;
 static kh_to_t*     locale_to;
+static kh_strings_t* const_strings;
 
 
 void init_hash_helper() {
@@ -29,6 +31,7 @@ void init_hash_helper() {
     hash_to = kh_init(to);
     locale_from = kh_init(from);
     locale_to = kh_init(to);
+    const_strings = kh_init(strings);
     pthread_rwlock_init(&hash_lock, NULL);
     hash_running = 1;
 }
@@ -43,6 +46,8 @@ void fini_hash_helper() {
     locale_from = NULL;
     kh_destroy(to, locale_to);
     locale_to = NULL;
+    kh_destroy(strings, const_strings); //TODO: does not free memory correctly
+    const_strings = NULL;
     pthread_rwlock_destroy(&hash_lock);
 }
 
@@ -280,4 +285,44 @@ ptr_t to_locale_d(void* p) {
     }
     pthread_rwlock_unlock(&hash_lock);
     return ret;
+}
+
+char* from_cstring(ptr_t p) {
+    return (char*)from_ptrv(p);
+}
+
+ptr_t to_cstring(char* p) {
+    if((uintptr_t)p<0x100000000LL)
+        return to_ptrv(p);
+    ptr_t ret = 0;
+    pthread_rwlock_rdlock(&hash_lock);
+    khint_t k = kh_get(strings, const_strings, p);
+    if(k==kh_end(const_strings)) {
+        // create a new key, but need write lock!
+        pthread_rwlock_unlock(&hash_lock);
+        pthread_rwlock_wrlock(&hash_lock);
+        ret = to_ptrv(box_strdup(p));
+        int r;
+        k = kh_put(strings, const_strings, (char*)from_ptrv(ret), &r);
+    } else
+        ret = to_ptrv(kh_value(const_strings, k));
+    pthread_rwlock_unlock(&hash_lock);
+    return ret;
+}
+
+ptr_t to_cstring_d(char* p) {
+    if((uintptr_t)p<0x100000000LL)
+        return to_ptrv(p);
+    ptr_t ret = 0;
+    pthread_rwlock_wrlock(&hash_lock);
+    khint_t k = kh_get(strings, const_strings, p);
+    if(k==kh_end(const_strings)) {
+        // assert?
+    } else {
+        ret = to_ptrv(kh_value(const_strings, k));
+        kh_del(strings, const_strings, k);
+        free(from_ptrv(ret));
+    }
+    pthread_rwlock_unlock(&hash_lock);
+    return ret;
 }
\ No newline at end of file