]> git.ipfire.org Git - people/ms/strongswan.git/blobdiff - src/libstrongswan/utils/utils.c
atomics: Move atomics/recounting support to separate files
[people/ms/strongswan.git] / src / libstrongswan / utils / utils.c
index 8ef9a1f338002793ca15920fb1b8ff0924cd7626..8a52d04c595fffacf2f02ada70b277bee337643f 100644 (file)
@@ -39,7 +39,6 @@
 #include <utils/debug.h>
 #include <utils/chunk.h>
 #include <collections/enumerator.h>
-#include <threading/spinlock.h>
 #include <threading/mutex.h>
 #include <threading/condvar.h>
 
@@ -58,6 +57,50 @@ ENUM(status_names, SUCCESS, NEED_MORE,
        "NEED_MORE",
 );
 
+/**
+ * Described in header.
+ */
+void* malloc_align(size_t size, u_int8_t align)
+{
+       u_int8_t pad;
+       void *ptr;
+
+       if (align == 0)
+       {
+               align = 1;
+       }
+       ptr = malloc(align + sizeof(pad) + size);
+       if (!ptr)
+       {
+               return NULL;
+       }
+       /* store padding length just before data, down to the allocation boundary
+        * to do some verification during free_align() */
+       pad = align - ((uintptr_t)ptr % align);
+       memset(ptr, pad, pad);
+       return ptr + pad;
+}
+
+/**
+ * Described in header.
+ */
+void free_align(void *ptr)
+{
+       u_int8_t pad, *pos;
+
+       pos = ptr - 1;
+       /* verify padding to check any corruption */
+       for (pad = *pos; (void*)pos >= ptr - pad; pos--)
+       {
+               if (*pos != pad)
+               {
+                       DBG1(DBG_LIB, "!!!! invalid free_align() !!!!");
+                       return;
+               }
+       }
+       free(ptr - pad);
+}
+
 /**
  * Described in header.
  */
@@ -109,6 +152,25 @@ void memwipe_noinline(void *ptr, size_t n)
        memwipe_inline(ptr, n);
 }
 
+/**
+ * Described in header.
+ */
+bool memeq_const(const void *x, const void *y, size_t len)
+{
+       const u_char *a, *b;
+       u_int bad = 0;
+       size_t i;
+
+       a = (const u_char*)x;
+       b = (const u_char*)y;
+
+       for (i = 0; i < len; i++)
+       {
+               bad |= a[i] != b[i];
+       }
+       return !bad;
+}
+
 /**
  * Described in header.
  */
@@ -247,7 +309,7 @@ static mutex_t *sigint_mutex;
 /**
  * Control handler to catch ^C
  */
-static BOOL handler(DWORD dwCtrlType)
+static BOOL WINAPI handler(DWORD dwCtrlType)
 {
        switch (dwCtrlType)
        {
@@ -661,78 +723,6 @@ void nop()
 {
 }
 
-#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
-
-/**
- * Spinlock for ref_get/put
- */
-static spinlock_t *ref_lock;
-
-/**
- * Increase refcount
- */
-refcount_t ref_get(refcount_t *ref)
-{
-       refcount_t current;
-
-       ref_lock->lock(ref_lock);
-       current = ++(*ref);
-       ref_lock->unlock(ref_lock);
-
-       return current;
-}
-
-/**
- * Decrease refcount
- */
-bool ref_put(refcount_t *ref)
-{
-       bool more_refs;
-
-       ref_lock->lock(ref_lock);
-       more_refs = --(*ref) > 0;
-       ref_lock->unlock(ref_lock);
-       return !more_refs;
-}
-
-/**
- * Current refcount
- */
-refcount_t ref_cur(refcount_t *ref)
-{
-       refcount_t current;
-
-       ref_lock->lock(ref_lock);
-       current = *ref;
-       ref_lock->unlock(ref_lock);
-
-       return current;
-}
-
-/**
- * Spinlock for all compare and swap operations.
- */
-static spinlock_t *cas_lock;
-
-/**
- * Compare and swap if equal to old value
- */
-#define _cas_impl(name, type) \
-bool cas_##name(type *ptr, type oldval, type newval) \
-{ \
-       bool swapped; \
-       cas_lock->lock(cas_lock); \
-       if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
-       cas_lock->unlock(cas_lock); \
-       return swapped; \
-}
-
-_cas_impl(bool, bool)
-_cas_impl(ptr, void*)
-
-#endif /* !HAVE_GCC_ATOMIC_OPERATIONS && !HAVE_GCC_SYNC_OPERATIONS */
-
-
 #ifdef HAVE_FMEMOPEN_FALLBACK
 
 static int fmemread(chunk_t *cookie, char *buf, int size)
@@ -785,12 +775,7 @@ void utils_init()
 #ifdef WIN32
        windows_init();
 #endif
-
-#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
-       ref_lock = spinlock_create();
-       cas_lock = spinlock_create();
-#endif
-
+       atomics_init();
        strerror_init();
 }
 
@@ -802,12 +787,7 @@ void utils_deinit()
 #ifdef WIN32
        windows_deinit();
 #endif
-
-#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
-       ref_lock->destroy(ref_lock);
-       cas_lock->destroy(cas_lock);
-#endif
-
+       atomics_deinit();
        strerror_deinit();
 }