1/* Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
2 *
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE COMPUTER, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE COMPUTER,
7 * INC.  ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
10 *
11 * falloc.c - FEE malloc routines
12 *
13 * Revision History
14 * ----------------
15 *  28 May 98	Doug Mitchell at Apple
16 *	Added Mac-specific allocators from temp memory
17 *  20 Aug 96	Doug Mitchell at NeXT
18 *	Created.
19 */
20
21#include "platform.h"
22#include "falloc.h"
23#include <stdlib.h>
24
25/* watchpoint emulator */
26#define FALLOC_WATCH	0
27#if		FALLOC_WATCH
28#include <stdio.h>
29/* set these with debugger */
30void *mallocWatchAddrs;
31void *freeWatchAddrs;
32#endif
33
34/* if NULL, use our own */
35static mallocExternFcn *mallocExt = NULL;
36static freeExternFcn *freeExt = NULL;
37static reallocExternFcn *reallocExt = NULL;
38
39void fallocRegister(mallocExternFcn *mallocExtern,
40	freeExternFcn *freeExtern,
41	reallocExternFcn *reallocExtern)
42{
43	mallocExt = mallocExtern;
44	freeExt = freeExtern;
45	reallocExt = reallocExtern;
46}
47
48/*
49 * All this can be optimized and tailored to specific platforms, of course...
50 */
51
52void *fmalloc(unsigned size)
53{
54	void *rtn;
55	if(mallocExt != NULL) {
56		rtn = (mallocExt)(size);
57	}
58	else {
59		rtn = malloc(size);
60	}
61	#if		FALLOC_WATCH
62	if(rtn == mallocWatchAddrs) {
63		printf("====fmalloc watchpoint (0x%x) hit\n",
64			(unsigned)mallocWatchAddrs);
65	}
66	#endif
67	return rtn;
68}
69
70void *fmallocWithData(const void *origData,
71	unsigned origDataLen)
72{
73	void *rtn = fmalloc(origDataLen);
74
75	bcopy(origData, rtn, origDataLen);
76	return rtn;
77}
78
79void ffree(void *data)
80{
81	#if		FALLOC_WATCH
82	if(data == freeWatchAddrs) {
83		printf("====ffree watchpoint (0x%x) hit\n",
84			(unsigned)freeWatchAddrs);
85	}
86	#endif
87	if(freeExt != NULL) {
88		(freeExt)(data);
89	}
90	else {
91		free(data);
92	}
93}
94
95void *frealloc(void *oldPtr, unsigned newSize)
96{
97	#if		FALLOC_WATCH
98	if(oldPtr == freeWatchAddrs) {
99		printf("====frealloc watchpoint (0x%x) hit\n",
100			(unsigned)freeWatchAddrs);
101	}
102	#endif
103	if(reallocExt != NULL) {
104		return (reallocExt)(oldPtr, newSize);
105	}
106	else {
107		return realloc(oldPtr, newSize);
108	}
109}
110