]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - gmqcc.h
Major cleanup of platform/fs stuff
[xonotic/gmqcc.git] / gmqcc.h
diff --git a/gmqcc.h b/gmqcc.h
index 93178228b98c02847bc1df82caea0c7a94ba0b46..235059debecdebb9c00262a54eeeadc405bd80f1 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012, 2013
+ * Copyright (C) 2012, 2013, 2014, 2015
  *     Dale Weiler
  *     Wolfgang Bumiller
  *
@@ -25,6 +25,8 @@
 #define GMQCC_HDR
 #include <stdarg.h>
 #include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include <time.h>
 
 #define GMQCC_VERSION_MAJOR 0
@@ -178,20 +180,15 @@ GMQCC_IND_STRING(GMQCC_VERSION_PATCH) \
 #define GMQCC_ARRAY_COUNT(X) (sizeof(X) / sizeof((X)[0]))
 
 /* stat.c */
-void  stat_info          (void);
-char *stat_mem_strdup    (const char *, size_t,         const char *, bool);
-void *stat_mem_reallocate(void *,       size_t, size_t, const char *, const char *);
-void  stat_mem_deallocate(void *);
-void *stat_mem_allocate  (size_t, size_t, const char *, const char *);
+char *stat_mem_strdup(const char *, bool);
 
-#define mem_a(SIZE)              stat_mem_allocate  ((SIZE), __LINE__, __FILE__, #SIZE)
-#define mem_d(PTRN)              stat_mem_deallocate((void*)(PTRN))
-#define mem_r(PTRN, SIZE)        stat_mem_reallocate((void*)(PTRN), (SIZE), __LINE__, __FILE__, #SIZE)
-#define mem_af(SIZE, FILE, LINE) stat_mem_allocate  ((SIZE), (LINE), (FILE), #SIZE)
+#define mem_a(SIZE)              malloc(SIZE)
+#define mem_d(PTRN)              free((void*)PTRN)
+#define mem_r(PTRN, SIZE)        realloc((void*)PTRN, SIZE)
 
 /* TODO: rename to mem variations */
-#define util_strdup(SRC)         stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, false)
-#define util_strdupe(SRC)        stat_mem_strdup((char*)(SRC), __LINE__, __FILE__, true)
+#define util_strdup(SRC)         stat_mem_strdup((char*)(SRC), false)
+#define util_strdupe(SRC)        stat_mem_strdup((char*)(SRC), true)
 
 /* util.c */
 
@@ -213,9 +210,10 @@ void *stat_mem_allocate  (size_t, size_t, const char *, const char *);
 #define util_isprint(a) (((unsigned)(a)-0x20) < 0x5F)
 #define util_isspace(a) (((a) >= 9 && (a) <= 13) || (a) == ' ')
 
-bool  util_strupper      (const char *);
-bool  util_strdigit      (const char *);
-void  util_endianswap    (void *, size_t, unsigned int);
+bool  util_strupper(const char *);
+bool  util_strdigit(const char *);
+
+void  util_endianswap(void *, size_t, unsigned int);
 
 size_t util_strtocmd         (const char *, char *, size_t);
 size_t util_strtononcmd      (const char *, char *, size_t);
@@ -236,9 +234,7 @@ const char *util_strerror(int err);
 const struct tm *util_localtime(const time_t *timer);
 const char      *util_ctime    (const time_t *timer);
 
-typedef struct fs_file_s fs_file_t;
-
-bool             util_isatty(fs_file_t *);
+bool             util_isatty(FILE *);
 size_t           hash(const char *key);
 
 /*
@@ -257,15 +253,17 @@ typedef struct {
 
 /* hidden interface */
 void _util_vec_grow(void **a, size_t i, size_t s);
-#define GMQCC_VEC_WILLGROW(X,Y) ( \
+void _util_vec_delete(void *vec, size_t line, const char *file);
+
+#define GMQCC_VEC_WILLGROW(X, Y) ( \
     ((!(X) || vec_meta(X)->used + Y >= vec_meta(X)->allocated)) ? \
         (void)_util_vec_grow(((void**)&(X)), (Y), sizeof(*(X))) : \
         (void)0                                                   \
 )
 
 /* exposed interface */
-#define vec_meta(A)       (((vector_t*)((void*)A)) - 1)
-#define vec_free(A)       ((void)((A) ? (mem_d((void*)vec_meta(A)), (A) = NULL) : 0))
+#define vec_meta(A)       ((vector_t*)(((char *)(A)) - sizeof(vector_t)))
+#define vec_free(A)       ((void)((A) ? (_util_vec_delete((void *)(A), __LINE__, __FILE__), (A) = NULL) : 0))
 #define vec_push(A,V)     (GMQCC_VEC_WILLGROW((A),1), (A)[vec_meta(A)->used++] = (V))
 #define vec_size(A)       ((A) ? vec_meta(A)->used : 0)
 #define vec_add(A,N)      (GMQCC_VEC_WILLGROW((A),(N)), vec_meta(A)->used += (N), &(A)[vec_meta(A)->used-(N)])
@@ -327,55 +325,7 @@ int           util_snprintf(char *str, size_t, const char *fmt, ...);
 
 
 /* fs.c */
-#define FS_FILE_SEEK_SET  0
-#define FS_FILE_SEEK_CUR  1
-#define FS_FILE_SEEK_END  2
-#define FS_FILE_EOF      -1
-
-typedef struct fs_dir_s  fs_dir_t;
-/*typedef struct fs_file_s fs_file_t;*/
-typedef struct dirent    fs_dirent_t;
-
-void           fs_file_close  (fs_file_t *);
-int            fs_file_error  (fs_file_t *);
-int            fs_file_getc   (fs_file_t *);
-int            fs_file_printf (fs_file_t *, const char *, ...);
-int            fs_file_puts   (fs_file_t *, const char *);
-int            fs_file_seek   (fs_file_t *, long int, int);
-long           fs_file_tell   (fs_file_t *);
-int            fs_file_flush  (fs_file_t *);
-
-size_t         fs_file_read   (void *,        size_t, size_t, fs_file_t *);
-size_t         fs_file_write  (const void *,  size_t, size_t, fs_file_t *);
-
-fs_file_t     *fs_file_open   (const char *, const char *);
-int            fs_file_getline(char  **, size_t *, fs_file_t *);
-
-int            fs_dir_make    (const char *);
-fs_dir_t      *fs_dir_open    (const char *);
-int            fs_dir_close   (fs_dir_t *);
-fs_dirent_t   *fs_dir_read    (fs_dir_t *);
-
-
-/* correct.c */
-typedef struct correct_trie_s {
-    void                  *value;
-    struct correct_trie_s *entries;
-} correct_trie_t;
-
-correct_trie_t* correct_trie_new(void);
-
-typedef struct {
-    char   ***edits;
-    size_t  **lens;
-} correction_t;
-
-void  correct_del (correct_trie_t*, size_t **);
-void  correct_add (correct_trie_t*, size_t ***, const char *);
-char *correct_str (correction_t *, correct_trie_t*, const char *);
-void  correct_init(correction_t *);
-void  correct_free(correction_t *);
-
+int fs_file_getline(char  **, size_t *, FILE *);
 
 /* code.c */
 
@@ -626,6 +576,11 @@ enum {
 /* TODO: elide */
 extern const char *util_instr_str[VINSTR_END];
 
+void util_swap_header     (prog_header_t              *code_header);
+void util_swap_statements (prog_section_statement_t   *statements);
+void util_swap_defs_fields(prog_section_both_t        *section);
+void util_swap_functions  (prog_section_function_t    *functions);
+void util_swap_globals    (int32_t                    *globals);
 
 typedef float    qcfloat_t;
 typedef int32_t  qcint_t;
@@ -693,8 +648,8 @@ enum {
     LVL_ERROR
 };
 
-fs_file_t *con_default_out(void);
-fs_file_t *con_default_err(void);
+FILE *con_default_out(void);
+FILE *con_default_err(void);
 
 void con_vprintmsg (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap);
 void con_printmsg  (int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...);
@@ -705,7 +660,6 @@ void con_close (void);
 void con_init  (void);
 void con_reset (void);
 void con_color (int);
-int  con_change(const char *, const char *);
 int  con_verr  (const char *, va_list);
 int  con_vout  (const char *, va_list);
 int  con_err   (const char *, ...);
@@ -781,17 +735,17 @@ typedef struct {
 } qc_exec_stack_t;
 
 typedef struct qc_program_s {
-    char                    *filename;
+    char                     *filename;
     prog_section_statement_t *code;
     prog_section_def_t       *defs;
     prog_section_def_t       *fields;
     prog_section_function_t  *functions;
-    char                    *strings;
-    qcint_t                   *globals;
-    qcint_t                   *entitydata;
-    bool                    *entitypool;
+    char                     *strings;
+    qcint_t                  *globals;
+    qcint_t                  *entitydata;
+    bool                     *entitypool;
 
-    const char*             *function_stack;
+    const char*              *function_stack;
 
     uint16_t crc16;
 
@@ -817,6 +771,20 @@ typedef struct qc_program_s {
     size_t xflags;
 
     int    argc; /* current arg count for debugging */
+
+    /* cached fields */
+    struct {
+        qcint_t frame;
+        qcint_t nextthink;
+        qcint_t think;
+    } cached_fields;
+
+    struct {
+        qcint_t self;
+        qcint_t time;
+    } cached_globals;
+
+    bool supports_state; /* is INSTR_STATE supported? */
 } qc_program_t;
 
 qc_program_t*       prog_load      (const char *filename, bool ignoreversion);
@@ -965,7 +933,7 @@ typedef struct {
 
 extern opts_cmd_t opts;
 
-#define OPTS_GENERIC(f,i)    (!! (((f)[(i)/32]) & (1<< (unsigned)((i)%32))))
+#define OPTS_GENERIC(f,i)    (!! (((f)[(i)/32]) & (1<< (unsigned)((i)%32))))
 
 #define OPTS_FLAG(i)         OPTS_GENERIC(opts.flags,        (i))
 #define OPTS_WARN(i)         OPTS_GENERIC(opts.warn,         (i))