summaryrefslogtreecommitdiff
path: root/src/libstddjb/alloc.c
blob: e3f89dd592dae6cd65a4e201e96c5e5ddf206576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* ISC license. */

#include <errno.h>
#include <stdlib.h>
#include <skalibs/sysdeps.h>
#include <skalibs/alloc.h>
#include "alloc-internal.h"

#ifdef DEBUG_ALLOC
# include "buffer.h"
# include "strerr2.h"
# include "lolstdio.h"
# define PLM(...) (bprintf(buffer_2, "%s: debug_alloc: ", PROG), bprintf(buffer_2, __VA_ARGS__), buffer_putflush(buffer_2, "\n", 1))
#endif

aligned_char_ref alloc (unsigned int n)
{
  register aligned_char_ref p = n ? (aligned_char_ref)malloc(n) : (aligned_char_ref)alloc_0 ;
#ifdef DEBUG_ALLOC
  static unsigned int counter = 0 ;
  PLM("alloc(%u): %p. Allocated: %u", n, p, ++counter) ;
#endif
  return p ;
}

void alloc_free (void *p)
{
  register int e = errno ;
#ifdef DEBUG_ALLOC
  static unsigned int counter = 0 ;
  PLM("alloc_free(%p). Freed: %u", p, ++counter) ;
#endif
#ifndef SKALIBS_HASMALLOC0
  if (p != alloc_0)
#endif
  free(p) ;
  errno = e ;
}

int alloc_realloc (aligned_char_ref *x, unsigned int n)
{
  aligned_char_ref y = n ? (aligned_char_ref)realloc(*x, n) : (free(*x), alloc_0) ;
#ifdef DEBUG_ALLOC
  PLM("alloc_realloc(&%p) -> new address = %p", *x, y) ;
#endif
  if (!y) return 0 ;
  *x = y ;
  return 1 ;
}