1 /* nasmlib.h header file for nasmlib.c
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
10 #define NASM_NASMLIB_H
13 * If this is defined, the wrappers around malloc et al will
14 * transform into logging variants, which will cause NASM to create
15 * a file called `malloc.log' when run, and spew details of all its
16 * memory management into that. That can then be analysed to detect
17 * memory leaks and potentially other problems too.
19 /* #define LOGALLOC */
22 * Wrappers around malloc, realloc and free. nasm_malloc will
23 * fatal-error and die rather than return NULL; nasm_realloc will
24 * do likewise, and will also guarantee to work right on being
25 * passed a NULL pointer; nasm_free will do nothing if it is passed
28 #ifdef NASM_NASM_H /* need efunc defined for this */
29 void nasm_set_malloc_error (efunc
);
31 void *nasm_malloc (size_t);
32 void *nasm_realloc (void *, size_t);
33 void nasm_free (void *);
34 char *nasm_strdup (char *);
35 char *nasm_strndup (char *, size_t);
37 void *nasm_malloc_log (char *, int, size_t);
38 void *nasm_realloc_log (char *, int, void *, size_t);
39 void nasm_free_log (char *, int, void *);
40 char *nasm_strdup_log (char *, int, char *);
41 char *nasm_strndup_log (char *, int, char *, size_t);
42 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
43 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
44 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
45 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
46 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
51 * ANSI doesn't guarantee the presence of `stricmp' or
54 int nasm_stricmp (const char *, const char *);
55 int nasm_strnicmp (const char *, const char *, int);
58 * Convert a string into a number, using NASM number rules. Sets
59 * `*error' to TRUE if an error occurs, and FALSE otherwise.
61 long readnum(char *str
, int *error
);
64 * Convert a character constant into a number. Sets
65 * `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
66 * str points to and length covers the middle of the string,
69 long readstrnum(char *str
, int length
, int *warn
);
72 * seg_init: Initialise the segment-number allocator.
73 * seg_alloc: allocate a hitherto unused segment number.
79 * many output formats will be able to make use of this: a standard
80 * function to add an extension to the name of the input file
83 void standard_extension (char *inname
, char *outname
, char *extension
,
88 * some handy macros that will probably be of use in more than one
89 * output format: convert integers into little-endian byte packed
93 #define WRITELONG(p,v) \
95 *(p)++ = (unsigned char)((v) & 0xFF); \
96 *(p)++ = (unsigned char)(((v) >> 8) & 0xFF); \
97 *(p)++ = (unsigned char)(((v) >> 16) & 0xFF); \
98 *(p)++ = (unsigned char)(((v) >> 24) & 0xFF); \
101 #define WRITESHORT(p,v) \
103 *(p)++ = (unsigned char)((v) & 0xFF); \
104 *(p)++ = (unsigned char)(((v) >> 8) & 0xFF); \
108 * and routines to do the same thing to a file
110 void fwriteshort (int data
, FILE *fp
);
111 void fwritelong (long data
, FILE *fp
);
114 * Routines to manage a dynamic random access array of longs which
115 * may grow in size to be more than the largest single malloc'able
119 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
120 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
122 typedef struct RAA RAA
;
123 typedef union RAA_UNION RAA_UNION
;
124 typedef struct RAA_LEAF RAA_LEAF
;
125 typedef struct RAA_BRANCH RAA_BRANCH
;
129 * Number of layers below this one to get to the real data. 0
130 * means this structure is a leaf, holding RAA_BLKSIZE real
131 * data items; 1 and above mean it's a branch, holding
132 * RAA_LAYERSIZE pointers to the next level branch or leaf
137 * Number of real data items spanned by one position in the
138 * `data' array at this level. This number is 1, trivially, for
139 * a leaf (level 0): for a level 1 branch it should be
140 * RAA_BLKSIZE, and for a level 2 branch it's
141 * RAA_LAYERSIZE*RAA_BLKSIZE.
146 long data
[RAA_BLKSIZE
];
149 struct RAA
*data
[RAA_LAYERSIZE
];
155 struct RAA
*raa_init (void);
156 void raa_free (struct RAA
*);
157 long raa_read (struct RAA
*, long);
158 struct RAA
*raa_write (struct RAA
*r
, long posn
, long value
);
161 * Routines to manage a dynamic sequential-access array, under the
162 * same restriction on maximum mallocable block. This array may be
163 * written to in two ways: a contiguous chunk can be reserved of a
164 * given size, and a pointer returned, or single-byte data may be
165 * written. The array can also be read back in the same two ways:
166 * as a series of big byte-data blocks or as a list of structures
172 * members `end' and `elem_len' are only valid in first link in
173 * list; `rptr' and `rpos' are used for reading
175 struct SAA
*next
, *end
, *rptr
;
176 long elem_len
, length
, posn
, start
, rpos
;
180 struct SAA
*saa_init (long elem_len
); /* 1 == byte */
181 void saa_free (struct SAA
*);
182 void *saa_wstruct (struct SAA
*); /* return a structure of elem_len */
183 void saa_wbytes (struct SAA
*, void *, long); /* write arbitrary bytes */
184 void saa_rewind (struct SAA
*); /* for reading from beginning */
185 void *saa_rstruct (struct SAA
*); /* return NULL on EOA */
186 void *saa_rbytes (struct SAA
*, long *); /* return 0 on EOA */
187 void saa_rnbytes (struct SAA
*, void *, long); /* read a given no. of bytes */
188 void saa_fread (struct SAA
*s
, long posn
, void *p
, long len
); /* fixup */
189 void saa_fwrite (struct SAA
*s
, long posn
, void *p
, long len
); /* fixup */
190 void saa_fpwrite (struct SAA
*, FILE *);
196 extern char *stdscan_bufptr
;
197 void stdscan_reset(void);
198 int stdscan (void *private_data
, struct tokenval
*tv
);
203 * Library routines to manipulate expression data types.
205 int is_reloc(expr
*);
206 int is_simple(expr
*);
207 int is_really_simple (expr
*);
208 int is_unknown(expr
*);
209 int is_just_unknown(expr
*);
210 long reloc_value(expr
*);
211 long reloc_seg(expr
*);
212 long reloc_wrt(expr
*);
216 * Binary search routine. Returns index into `array' of an entry
217 * matching `string', or <0 if no match. `array' is taken to
218 * contain `size' elements.
220 int bsi (char *string
, char **array
, int size
);
223 char *src_set_fname(char *newname
);
224 long src_set_linnum(long newline
);
225 long src_get_linnum(void);
227 * src_get may be used if you simply want to know the source file and line.
228 * It is also used if you maintain private status about the source location
229 * It return 0 if the information was the same as the last time you
230 * checked, -1 if the name changed and (new-old) if just the line changed.
232 int src_get(long *xline
, char **xname
);
234 void nasm_quote(char **str
);
235 char *nasm_strcat(char *one
, char *two
);
236 void nasmlib_cleanup(void);
238 void null_debug_routine();
239 extern struct dfmt null_debug_form
;
240 extern struct dfmt
*null_debug_arr
[2];