Skip to content
Myr

jv_alloc.c

This file

Path

src/jv_alloc.c

189 lines · 4.4 KiB · C

Last changed by

Fix warning: a function definition without a prototype is deprecated (#3259)itchynybefore adoptiona8f27cc

History of this file

Take it

1#include <assert.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include "jv.h"67struct nomem_handler {8    jv_nomem_handler_f handler;9    void *data;10};1112#if !defined(HAVE_PTHREAD_KEY_CREATE) || \13    !defined(HAVE_PTHREAD_ONCE) || \14    !defined(HAVE_ATEXIT)1516/* Try thread-local storage? */1718#ifdef _MSC_VER19/* Visual C++: yes */20static __declspec(thread) struct nomem_handler nomem_handler;21#define USE_TLS22#else23#ifdef HAVE___THREAD24/* GCC and friends: yes */25static __thread struct nomem_handler nomem_handler;26#define USE_TLS27#endif /* HAVE___THREAD */28#endif /* _MSC_VER */2930#endif /* !HAVE_PTHREAD_KEY_CREATE */3132#ifdef USE_TLS33void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {34  nomem_handler.handler = handler;35}3637static void memory_exhausted(void) {38  if (nomem_handler.handler)39    nomem_handler.handler(nomem_handler.data); // Maybe handler() will longjmp() to safety40  // Or not41  fprintf(stderr, "jq: error: cannot allocate memory\n");42  abort();43}44#else /* USE_TLS */4546#ifdef HAVE_PTHREAD_KEY_CREATE47#include <pthread.h>4849static pthread_key_t nomem_handler_key;50static pthread_once_t mem_once = PTHREAD_ONCE_INIT;5152/* tsd_fini is called on application exit */53/* it clears the nomem_handler allocated in the main thread */54static void tsd_fini(void) {55  struct nomem_handler *nomem_handler;56  nomem_handler = pthread_getspecific(nomem_handler_key);57  if (nomem_handler) {58    (void) pthread_setspecific(nomem_handler_key, NULL);59    free(nomem_handler);60  }61}6263/* The tsd_fini_thread is a destructor set by calling */64/* pthread_key_create(&nomem_handler_key, tsd_fini_thread) */65/* It is called when thread ends */66static void tsd_fini_thread(void *nomem_handler) {67  free(nomem_handler);68}6970static void tsd_init(void) {71  if (pthread_key_create(&nomem_handler_key, tsd_fini_thread) != 0) {72    fprintf(stderr, "jq: error: cannot create thread specific key");73    abort();74  }75  if (atexit(tsd_fini) != 0) {76    fprintf(stderr, "jq: error: cannot set an exit handler");77    abort();78  }79}8081static void tsd_init_nomem_handler(void)82{83  if (pthread_getspecific(nomem_handler_key) == NULL) {84    struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));85    if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {86      fprintf(stderr, "jq: error: cannot set thread specific data");87      abort();88    }89  }90}9192void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {93  pthread_once(&mem_once, tsd_init); // cannot fail94  tsd_init_nomem_handler();9596  struct nomem_handler *nomem_handler;9798  nomem_handler = pthread_getspecific(nomem_handler_key);99  if (nomem_handler == NULL) {100    handler(data);101    fprintf(stderr, "jq: error: cannot allocate memory\n");102    abort();103  }104  nomem_handler->handler = handler;105  nomem_handler->data = data;106}107108static void memory_exhausted(void) {109  struct nomem_handler *nomem_handler;110111  pthread_once(&mem_once, tsd_init);112  tsd_init_nomem_handler();113114  nomem_handler = pthread_getspecific(nomem_handler_key);115  if (nomem_handler && nomem_handler->handler)116    nomem_handler->handler(nomem_handler->data); // Maybe handler() will longjmp() to safety117  // Or not118  fprintf(stderr, "jq: error: cannot allocate memory\n");119  abort();120}121122#else123124/* No thread-local storage of any kind that we know how to handle */125126static struct nomem_handler nomem_handler;127void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {128  nomem_handler.handler = handler;129  nomem_handler.data = data;130}131132static void memory_exhausted(void) {133  fprintf(stderr, "jq: error: cannot allocate memory\n");134  abort();135}136137#endif /* HAVE_PTHREAD_KEY_CREATE */138#endif /* USE_TLS */139140141void* jv_mem_alloc(size_t sz) {142  void* p = malloc(sz);143  if (!p) {144    memory_exhausted();145  }146  return p;147}148149void* jv_mem_alloc_unguarded(size_t sz) {150  return malloc(sz);151}152153void* jv_mem_calloc(size_t nemb, size_t sz) {154  assert(nemb > 0 && sz > 0);155  void* p = calloc(nemb, sz);156  if (!p) {157    memory_exhausted();158  }159  return p;160}161162void* jv_mem_calloc_unguarded(size_t nemb, size_t sz) {163  assert(nemb > 0 && sz > 0);164  return calloc(nemb, sz);165}166167char* jv_mem_strdup(const char *s) {168  char *p = strdup(s);169  if (!p) {170    memory_exhausted();171  }172  return p;173}174175char* jv_mem_strdup_unguarded(const char *s) {176  return strdup(s);177}178179void jv_mem_free(void* p) {180  free(p);181}182183void* jv_mem_realloc(void* p, size_t sz) {184  p = realloc(p, sz);185  if (!p) {186    memory_exhausted();187  }188  return p;189}