| 1 |
/* $XConsortium: util.c,v 1.6 92/03/26 16:42:26 eswu Exp $ */ |
| 2 |
/* Copyright International Business Machines,Corp. 1991 |
| 3 |
* All Rights Reserved |
| 4 |
* |
| 5 |
* License to use, copy, modify, and distribute this software |
| 6 |
* and its documentation for any purpose and without fee is |
| 7 |
* hereby granted, provided that the above copyright notice |
| 8 |
* appear in all copies and that both that copyright notice and |
| 9 |
* this permission notice appear in supporting documentation, |
| 10 |
* and that the name of IBM not be used in advertising or |
| 11 |
* publicity pertaining to distribution of the software without |
| 12 |
* specific, written prior permission. |
| 13 |
* |
| 14 |
* IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES |
| 15 |
* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT |
| 16 |
* LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF |
| 18 |
* THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND |
| 19 |
* PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT |
| 20 |
* OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF |
| 21 |
* THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES |
| 22 |
* THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN |
| 23 |
* NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 24 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 25 |
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 26 |
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 27 |
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 28 |
* SOFTWARE. |
| 29 |
*/ |
| 30 |
/* Author: Katherine A. Hitchcock IBM Almaden Research Laboratory */ |
| 31 |
|
| 32 |
#include "types.h" |
| 33 |
#include <stdio.h> |
| 34 |
#include "util.h" |
| 35 |
#include "fontmisc.h" |
| 36 |
|
| 37 |
static char *vm_base = NULL; /* Start of virtual memory area */ |
| 38 |
char *vm_next = NULL; /* Pointer to first free byte */ |
| 39 |
int32_t vm_free = 0; /* Count of free bytes */ |
| 40 |
int32_t vm_size = 0; /* Total size of memory */ |
| 41 |
|
| 42 |
/* |
| 43 |
* Initialize memory. |
| 44 |
*/ |
| 45 |
|
| 46 |
boolean vm_init(void) |
| 47 |
{ |
| 48 |
vm_next = vm_base = (char *) Xalloc(MAX_STRING_LEN); |
| 49 |
|
| 50 |
if (vm_base != NULL) { |
| 51 |
vm_free = MAX_STRING_LEN; |
| 52 |
vm_size = MAX_STRING_LEN; |
| 53 |
tokenMaxP = vm_next_byte() + MAX_STRING_LEN; |
| 54 |
return(TRUE); |
| 55 |
} |
| 56 |
else |
| 57 |
return(FALSE); |
| 58 |
} |
| 59 |
|
| 60 |
char *vm_alloc(unsigned int bytes) |
| 61 |
{ |
| 62 |
char *answer; |
| 63 |
|
| 64 |
/* Align returned bytes to 64-bit boundary */ |
| 65 |
bytes = (bytes + 7) & ~7; |
| 66 |
|
| 67 |
/* take space from what is left otherwise allocate another CHUNK */ |
| 68 |
if (bytes > vm_free) |
| 69 |
if (!vm_init()) return NULL; |
| 70 |
if (bytes <= vm_free) { |
| 71 |
answer = vm_next; |
| 72 |
vm_free -= bytes; |
| 73 |
vm_next += bytes; |
| 74 |
return answer; |
| 75 |
} |
| 76 |
else { |
| 77 |
printf("Trying to allocate %d bytes (%d maximum)\n", bytes, MAX_STRING_LEN); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Format an Integer object |
| 84 |
*/ |
| 85 |
void objFormatInteger(psobj *objP, int value) |
| 86 |
{ |
| 87 |
if (objP != NULL) { |
| 88 |
objP->type = OBJ_INTEGER; |
| 89 |
objP->len = 0; |
| 90 |
objP->data.integer = value; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Format a Real object |
| 96 |
*/ |
| 97 |
void objFormatReal(psobj *objP, float value) |
| 98 |
{ |
| 99 |
if (objP != NULL) { |
| 100 |
objP->type = OBJ_REAL; |
| 101 |
objP->len = 0; |
| 102 |
objP->data.real = value; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Format a Boolean object |
| 108 |
*/ |
| 109 |
void objFormatBoolean(psobj *objP, boolean value) |
| 110 |
{ |
| 111 |
if (objP != NULL) { |
| 112 |
objP->type = OBJ_BOOLEAN; |
| 113 |
objP->len = 0; |
| 114 |
objP->data.boolean = value; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* Format an Encoding object |
| 120 |
*/ |
| 121 |
void objFormatEncoding(psobj *objP, int length, psobj *valueP) |
| 122 |
{ |
| 123 |
if (objP != NULL) { |
| 124 |
objP->type = OBJ_ENCODING; |
| 125 |
objP->len = length; |
| 126 |
objP->data.arrayP = valueP; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* Format an Array object |
| 132 |
*/ |
| 133 |
void objFormatArray(psobj *objP, int length, psobj *valueP) |
| 134 |
{ |
| 135 |
if (objP != NULL) { |
| 136 |
objP->type = OBJ_ARRAY; |
| 137 |
objP->len = length; |
| 138 |
objP->data.arrayP = valueP; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/* |
| 144 |
* Format a String object |
| 145 |
*/ |
| 146 |
void objFormatString(psobj *objP, int length, char *valueP) |
| 147 |
{ |
| 148 |
if (objP != NULL) { |
| 149 |
objP->type = OBJ_STRING; |
| 150 |
objP->len = length; |
| 151 |
objP->data.valueP = valueP; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
* Format a Name object |
| 157 |
*/ |
| 158 |
void objFormatName(psobj *objP, int length, const char *valueP) |
| 159 |
{ |
| 160 |
if (objP != NULL) { |
| 161 |
objP->type = OBJ_NAME; |
| 162 |
objP->len = length; |
| 163 |
objP->data.nameP = valueP; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* Format a File object |
| 169 |
*/ |
| 170 |
void objFormatFile(psobj *objP, FILE *valueP) |
| 171 |
{ |
| 172 |
if (objP != NULL) { |
| 173 |
objP->type = OBJ_FILE; |
| 174 |
objP->len = 0; |
| 175 |
objP->data.fileP = valueP; |
| 176 |
} |
| 177 |
} |
| 178 |
|