xmalloc et al (NeXT)


[Home] Home > Programming > Portability >
xmalloc et al (NeXT)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>

#include <sys/types.h>

#include <objc/zone.h>

inline
void *
xzonemalloc(NXZone *zone, size_t size)
{
  register void *p = NULL;

  if ((p = NXZoneMalloc(zone, size)) == NULL) {
    perror("xzonemalloc");
    exit(EXIT_FAILURE);
  }

  bzero(p, size);

  return p;
}

inline
void *
xzonerealloc(NXZone *zone, void *ptr, size_t n)
{
  if ((ptr = NXZoneRealloc(zone, ptr, n)) == NULL) {
    perror("xzonerealloc");
    exit(EXIT_FAILURE);
  }
  bzero(ptr, n);

  return ptr;
}

inline
void *
xzonecalloc(NXZone *zone, size_t nelems, size_t elemsize)
{
  register void *newmem = NULL;

  newmem = NXZoneCalloc(zone,
                        nelems   ? nelems   : 1,
                        elemsize ? elemsize : 1);
  if (newmem == NULL) {
    perror("xzonecalloc");
    exit(EXIT_FAILURE);
  }

  return newmem;
}

void
xzonefree(NXZone *zone, void *ptr)
{
  if (ptr == NULL) {
    return;
  }

  NXZoneFree(zone, ptr);
}

/* ... */

#if defined(xmalloc)
# undef  xmalloc
#endif
#define xmalloc(__n) \
  xzonemalloc(NXDefaultMallocZone(), __n)

#if defined(xrealloc)
# undef xrealloc
#endif
#define xrealloc(__p, __n) \
  xzonerealloc(NXDefaultMallocZone(), __p, __n)

#if defined(xcalloc)
# undef xcalloc
#endif
#define xcalloc(__n, __e) \
  xzonecalloc(NXDefaultMallocZone(), __n, __e)

#if defined(xfree)
# undef xfree
#endif
#define xfree(__p) \
  xzonefree(NXDefaultMallocZone(), __p)

Copyright © 2010-2024 Paul Ward.
License Information