Edit

kc3-lang/kc3/libkc3/alloc.c

Branch :

  • libkc3/alloc.c
  • /* kc3
     * Copyright from 2022 to 2026 kmx.io <contact@kmx.io>
     *
     * Permission is hereby granted to use this software granted the above
     * copyright notice and this permission paragraph are included in all
     * copies and substantial portions of this software.
     *
     * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
     * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
     * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
     * THIS SOFTWARE.
     */
    #include "alloc.h"
    #include "assert.h"
    #include "env.h"
    #include "list.h"
    #include "memleak.h"
    #include "stacktrace.h"
    #include <stdlib.h>
    
    static thread_local bool g_alloc_memleak_lock = false;
    
    void * alloc (uw size)
    {
      void *a;
      s_env *env;
      s_list *stacktrace;
      if (! size) {
        err_puts("alloc: zero size");
        assert(! "alloc: zero size");
        return NULL;
      }
      a = calloc(1, size);
      if (! a) {
        err_puts("alloc: failed to allocate memory");
        assert(! "alloc: failed to allocate memory");
        return NULL;
      }
      if (g_memleak_enabled && ! g_alloc_memleak_lock) {
        g_alloc_memleak_lock = true;
        env = env_global();
        stacktrace = env ? stacktrace_get(env->stacktrace) : NULL;
        memleak_add(a, size, stacktrace);
        g_alloc_memleak_lock = false;
      }
      return a;
    }
    
    void alloc_free (void *ptr)
    {
      if (ptr) {
        if (g_memleak_enabled && ! g_alloc_memleak_lock) {
          g_alloc_memleak_lock = true;
          memleak_remove(ptr);
          g_alloc_memleak_lock = false;
        }
        free(ptr);
      }
    }