PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/src/z-virt.c

https://bitbucket.org/ekolis/jackband
C | 149 lines | 53 code | 30 blank | 66 comment | 18 complexity | a07a8821c225d3cf6c8c5ba55423a92f MD5 | raw file
  1/*
  2 * File: z-virt.c
  3 * Purpose: Memory management routines
  4 *
  5 * Copyright (c) 1997 Ben Harrison.
  6 *
  7 * This work is free software; you can redistribute it and/or modify it
  8 * under the terms of either:
  9 *
 10 * a) the GNU General Public License as published by the Free Software
 11 *    Foundation, version 2, or
 12 *
 13 * b) the "Angband licence":
 14 *    This software may be copied and distributed for educational, research,
 15 *    and not for profit purposes provided that this copyright and statement
 16 *    are included in all such copies.  Other copyrights may also apply.
 17 */
 18#include "z-virt.h"
 19#include "z-util.h"
 20
 21/*
 22 * Hooks for platform-specific memory allocation.
 23 */
 24static mem_alloc_hook ralloc_aux;
 25static mem_free_hook rnfree_aux;
 26static mem_realloc_hook realloc_aux;
 27
 28
 29/*
 30 * Set the hooks for the memory system.
 31 */
 32bool mem_set_hooks(mem_alloc_hook alloc, mem_free_hook free, mem_realloc_hook realloc)
 33{
 34	/* Error-check */
 35	if (!alloc || !free || !realloc) return FALSE;
 36
 37	/* Set up hooks */
 38	ralloc_aux = alloc;
 39	rnfree_aux = free;
 40	realloc_aux = realloc;
 41
 42	return TRUE;
 43}
 44
 45
 46/*
 47 * Allocate `len` bytes of memory.
 48 *
 49 * Returns:
 50 *  - NULL if `len` == 0; or
 51 *  - a pointer to a block of memory of at least `len` bytes
 52 *
 53 * Doesn't return on out of memory.
 54 */
 55void *mem_alloc(size_t len)
 56{
 57	void *mem;
 58
 59	/* Allow allocation of "zero bytes" */
 60	if (len == 0) return (NULL);
 61
 62	/* Allocate some memory */
 63	if (ralloc_aux) mem = (*ralloc_aux)(len);
 64	else            mem = malloc(len);
 65
 66	/* Handle OOM */
 67	if (!mem) quit("Out of Memory!");
 68
 69	return mem;
 70}
 71
 72
 73/*
 74 * Free the memory pointed to by `p`.
 75 *
 76 * Returns NULL.
 77 */
 78void *mem_free(void *p)
 79{
 80	/* Easy to free nothing */
 81	if (!p) return (NULL);
 82
 83	/* Free memory */
 84	if (rnfree_aux) (*rnfree_aux)(p);
 85	else            free(p);
 86
 87	/* Done */
 88	return (NULL);
 89}
 90
 91
 92/*
 93 * Allocate `len` bytes of memory, copying whatever is in `p` with it.
 94 *
 95 * Returns:
 96 *  - NULL if `len` == 0 or `p` is NULL; or
 97 *  - a pointer to a block of memory of at least `len` bytes
 98 *
 99 * Doesn't return on out of memory.
100 */
101void *mem_realloc(void *p, size_t len)
102{
103	void *mem;
104
105	/* Fail gracefully */
106	if (!p || len == 0) return (NULL);
107
108	if (realloc_aux) mem = (*realloc_aux)(p, len);
109	else             mem = realloc(p, len);
110
111	/* Handle OOM */
112	if (!mem) quit("Out of Memory!");
113
114	return mem;
115}
116
117
118
119/*
120 * Duplicates an existing string `str`, allocating as much memory as necessary.
121 */
122char *string_make(const char *str)
123{
124	char *res;
125	size_t siz;
126
127	/* Error-checking */
128	if (!str) return NULL;
129
130	/* Allocate space for the string (including terminator) */
131	siz = strlen(str) + 1;
132	res = mem_alloc(siz);
133
134	/* Copy the string (with terminator) */
135	my_strcpy(res, str, siz);
136
137	return res;
138}
139
140
141/*
142 * Un-allocate a string allocated above.
143 */
144#undef string_free
145char *string_free(char *str)
146{
147	/* Kill the buffer of chars we must have allocated above */
148	return mem_free(str);
149}