#import <stdlib.h>
#import <errno.h>
#import <objc/zone.h>
#import "zoneprotect.h"
#import "allocate.h"
#import "errors.h"
static NXZone *zone = NULL;
void *
allocate(
unsigned long size)
{
void *p;
int errnum;
if(size == 0)
return(NULL);
if(zone == NULL){
zone = NXCreateZone(vm_page_size, vm_page_size, 1);
if(zone == NULL){
errnum = errno;
system_error(errnum, "can't create NXZone (for internal data "
"structures)");
link_edit_error(DYLD_UNIX_RESOURCE, errnum, NULL);
halt();
exit(DYLD_EXIT_FAILURE_BASE + DYLD_UNIX_RESOURCE);
}
NXNameZone(zone, "dyld");
}
if((p = NXZoneMalloc(zone, size)) == NULL){
errnum = errno;
system_error(errnum, "can't allocate memory (for internal data "
"structures)");
link_edit_error(DYLD_UNIX_RESOURCE, errnum, NULL);
halt();
exit(DYLD_EXIT_FAILURE_BASE + DYLD_UNIX_RESOURCE);
}
return(p);
}
void *
allocate_with_no_error_check(
unsigned long size)
{
if(size == 0)
return(NULL);
if(zone == NULL){
zone = NXCreateZone(vm_page_size, vm_page_size, 1);
if(zone == NULL){
return(NULL);
}
NXNameZone(zone, "dyld");
}
return(NXZoneMalloc(zone, size));
}
void
protect_allocated_memory(
void)
{
#ifndef MALLOC_DEBUG
int errnum;
#endif
if(zone == NULL)
return;
#ifndef MALLOC_DEBUG
if(NXProtectZone(zone, NXZoneReadOnly))
return;
errnum = errno;
system_error(errnum, "can't protect allocated memory (for internal "
"data structures");
link_edit_error(DYLD_UNIX_RESOURCE, errnum, NULL);
halt();
exit(DYLD_EXIT_FAILURE_BASE + DYLD_UNIX_RESOURCE);
#endif
}
void
unprotect_allocated_memory(
void)
{
#ifndef MALLOC_DEBUG
int errnum;
#endif
if(zone == NULL)
return;
#ifndef MALLOC_DEBUG
if(NXProtectZone(zone, NXZoneReadWrite))
return;
errnum = errno;
system_error(errnum, "can't unprotect allocated memory (for internal "
"data structures");
link_edit_error(DYLD_UNIX_RESOURCE, errnum, NULL);
halt();
exit(DYLD_EXIT_FAILURE_BASE + DYLD_UNIX_RESOURCE);
#endif
}