]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/nibble-alloc.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * .../c-lib/src/nibble-alloc.c - fast mem allocation for decoded values
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
30 * If you modify this file, you must clearly indicate your changes.
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/nibble-alloc.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
37 * $Log: nibble-alloc.c,v $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
44 * Revision 1.1.1.1 1999/03/16 18:06:32 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.4 1997/02/28 13:39:51 wan
48 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
50 * Revision 1.3 1995/07/27 09:06:37 rj
51 * use memzero that is defined in .../snacc.h to use either memset or bzero.
53 * changed `_' to `-' in file names.
55 * Revision 1.2 1994/09/01 00:07:16 rj
56 * more portable .h file inclusion.
58 * Revision 1.1 1994/08/28 09:46:07 rj
59 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
63 #include "asn-config.h"
65 #if STDC_HEADERS || HAVE_STRING_H
67 /* An ANSI string.h and pre-ANSI memory.h might conflict. */
68 #if !STDC_HEADERS && HAVE_MEMORY_H
70 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
71 #endif /* not STDC_HEADERS and not HAVE_STRING_H */
73 #include "nibble-alloc.h"
76 NibbleMem
*nmG
= NULL
;
79 InitNibbleMem
PARAMS ((initialSize
, incrementSize
),
80 unsigned long int initialSize _AND_
81 unsigned long int incrementSize
)
85 nm
= (NibbleMem
*) malloc (sizeof (NibbleMem
));
86 nm
->incrementSize
= incrementSize
;
88 nm
->currNibbleBuf
= nm
->firstNibbleBuf
= (NibbleBuf
*)malloc (sizeof (NibbleBuf
));
89 nm
->firstNibbleBuf
->curr
= nm
->firstNibbleBuf
->start
= (char*) malloc (initialSize
);
90 nm
->firstNibbleBuf
->end
= nm
->firstNibbleBuf
->start
+ initialSize
;
91 nm
->firstNibbleBuf
->next
= NULL
;
92 memzero (nm
->currNibbleBuf
->start
, initialSize
);
94 nmG
= nm
;/* set global */
96 } /* InitNibbleAlloc */
100 * alloc new nibble buf, link in, reset to curr nibble buf
103 ServiceNibbleFault
PARAMS ((size
),
107 unsigned long newBufSize
;
111 if (size
> nm
->incrementSize
)
114 newBufSize
= nm
->incrementSize
;
116 nm
->currNibbleBuf
->next
= (NibbleBuf
*) malloc (sizeof (NibbleBuf
));
117 nm
->currNibbleBuf
= nm
->currNibbleBuf
->next
;
118 nm
->currNibbleBuf
->curr
= nm
->currNibbleBuf
->start
= (char*) malloc (newBufSize
);
119 nm
->currNibbleBuf
->end
= nm
->currNibbleBuf
->start
+ newBufSize
;
120 nm
->currNibbleBuf
->next
= NULL
;
121 memzero (nm
->currNibbleBuf
->start
, newBufSize
);
122 } /* serviceNibbleFault */
127 * returns requested space filled with zeros
130 NibbleAlloc
PARAMS ((size
),
139 if ((nm
->currNibbleBuf
->end
- nm
->currNibbleBuf
->curr
) < size
)
140 ServiceNibbleFault (size
);
142 retVal
= nm
->currNibbleBuf
->curr
;
145 * maintain word alignment
147 ndiff
= size
% sizeof (long);
150 nm
->currNibbleBuf
->curr
+= size
+ sizeof (long) - ndiff
;
153 * this is a fix from Terry Sullivan <FCLTPS@nervm.nerdc.ufl.edu>
155 * makes sure curr does not go past the end ptr
157 if (nm
->currNibbleBuf
->curr
> nm
->currNibbleBuf
->end
)
158 nm
->currNibbleBuf
->curr
= nm
->currNibbleBuf
->end
;
161 nm
->currNibbleBuf
->curr
+= size
;
169 * frees all nibble buffers except the first,
170 * resets the first to empty and zero's it
182 * reset first nibble buf
184 memzero (nm
->firstNibbleBuf
->start
, nm
->firstNibbleBuf
->curr
- nm
->firstNibbleBuf
->start
);
186 nm
->firstNibbleBuf
->curr
= nm
->firstNibbleBuf
->start
;
189 * free incrementally added nibble bufs
191 for (tmp
= nm
->firstNibbleBuf
->next
; tmp
!= NULL
; )
199 /* From ftp://ftp.cs.ubc.ca/pub/local/src/snacc/bugs-in-1.1 */
200 nm
->firstNibbleBuf
->next
= NULL
;
201 nm
->currNibbleBuf
= nm
->firstNibbleBuf
;
203 } /* ResetNibbleMem */
207 * frees all nibble buffers, closing this
208 * NibbleMem completely
222 for (tmp
= nm
->firstNibbleBuf
; tmp
!= NULL
; )
231 } /* ShutdownNibbleMem */