mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-22 12:53:47 +00:00
Estimated hours taken: 6
A general cleanup of the code in the runtime directory, aimed at
formulating a more coherent header file inclusion policy. This change
fixes a couple of bugs that prevented the runtime from compiling in
certain configurations (e.g. on muse) due to missing #includes, and
also fixes a few minor unrelated things such as the use of `size_t'
instead of `unsigned'.
Our header file inclusion policy is that every header file should
#include any other header files needed by the declarations or by the
macros it defines. Cyclic interface dependencies, where two header
files each #include the other, must be avoided (by splitting up header
files into smaller indepdent units, if necessary).
At some stage in the future we should rename all the header files to
`mercury_*.h', to avoid any possible name clashes with system or user
header files.
runtime/Mmake:
Add a new target `check_headers' to check that each
header file is syntactically valid in isolation.
runtime/imp.h:
runtime/mercury_float.h:
runtime/mercury_string.h:
runtime/mercury_types.h:
runtime/calls.h:
Move the code in "imp.h" into new header files.
"imp.h" now contains nothing but #includes.
runtime/conf.h.in:
runtime/*.h:
runtime/{label,prof,prof_mem}.c:
runtime/*.mod:
Update the #includes to reflect the new header file structure.
Add some missing header guards. Add some comments.
Put the correct years in most of the copyright notices.
runtime/heap.h:
Fix a bug: add #include "context.h", needed for
min_heap_reclamation_point.
runtime/context.h:
Fix a bug: add #include "memory.h", needed for MemoryZone.
Move the general description comment to the top of the file.
Fix the indentation of some comments. Add a couple of new comments.
runtime/context.mod:
Delete a couple of unnecessary declarations.
runtime/wrapper.mod:
Change the type used for memory sizes from `unsigned' to `size_t'.
Change the `-p' (primary cache size) option so that it is always
a size in kilobytes, rather than being schitzophrenic about
whether it is bytes or kilobytes.
runtime/regorder_base.h:
Removed, since it not used (and constitutes a
double-maintenance problem).
123 lines
2.4 KiB
C
123 lines
2.4 KiB
C
/*
|
|
** Copyright (C) 1993-1996 University of Melbourne.
|
|
** This file may only be copied under the terms of the GNU Library General
|
|
** Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
*/
|
|
|
|
/*
|
|
** Table handling module.
|
|
**
|
|
** This file supplies data manipulation routines to other modules;
|
|
** it does not store any data itself. Its routines are generic,
|
|
** applicable to the storage of any kind of data structure with
|
|
** a primary key and a hash function on it.
|
|
*/
|
|
|
|
#define HASHDEBUG
|
|
|
|
#include <stdio.h>
|
|
#include "std.h"
|
|
#include "dlist.h"
|
|
#include "table.h"
|
|
|
|
/*
|
|
** Initialize a table.
|
|
*/
|
|
|
|
void tab_init_table(Table *table)
|
|
{
|
|
reg int i;
|
|
|
|
table->ta_store = make_many(List *, table->ta_size);
|
|
|
|
for (i = 0; i < table->ta_size; i++)
|
|
table->ta_store[i] = NULL;
|
|
}
|
|
|
|
/*
|
|
** Look up and return the entry corresponding to the key
|
|
** in a table.
|
|
*/
|
|
|
|
void *tab_lookup_table(const Table *table, const void *key)
|
|
{
|
|
reg List *ptr;
|
|
reg int h;
|
|
|
|
h = tablehash(table)(key);
|
|
|
|
#ifdef HASHDEBUG
|
|
if (! (0 <= h && h < table->ta_size))
|
|
fprintf(stderr, "internal error: bad hash index in lookup_table: %d, table size %d\n", h, table->ta_size);
|
|
#endif
|
|
|
|
for_list (ptr, table->ta_store[h])
|
|
if (tableequal(table)(key, tablekey(table)(ldata(ptr))))
|
|
return ldata(ptr);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
** Insert a new entry into the table.
|
|
** Return whether it was there before.
|
|
*/
|
|
|
|
bool tab_insert_table(const Table *table, void *entry)
|
|
{
|
|
reg List *ptr;
|
|
reg const void *key;
|
|
reg int h;
|
|
|
|
key = tablekey(table)(entry);
|
|
h = tablehash(table)(key);
|
|
|
|
#ifdef HASHDEBUG
|
|
if (! (0 <= h && h < table->ta_size))
|
|
fprintf(stderr, "internal error: bad hash index in lookup_table: %d, table size %d\n", h, table->ta_size);
|
|
#endif
|
|
|
|
for_list (ptr, table->ta_store[h])
|
|
if (tableequal(table)(key, tablekey(table)(ldata(ptr))))
|
|
return TRUE;
|
|
|
|
table->ta_store[h] = addhead(table->ta_store[h], entry);
|
|
return FALSE;
|
|
}
|
|
|
|
/*
|
|
** Return all table entries in a list.
|
|
*/
|
|
|
|
List *tab_get_all_entries(const Table *table)
|
|
{
|
|
reg List *list;
|
|
reg int i;
|
|
|
|
list = makelist0();
|
|
for (i = 0; i < table->ta_size; i++)
|
|
addndlist(list, table->ta_store[i]);
|
|
|
|
return list;
|
|
}
|
|
|
|
/*
|
|
** Convert a string to a positive int. The return value
|
|
** mod the table size is a good hash value.
|
|
*/
|
|
|
|
int tab_str_to_int(const char *cs)
|
|
{
|
|
reg int h;
|
|
reg const char *s;
|
|
|
|
s = cs;
|
|
for (h = 0; *s != '\0'; s++)
|
|
h = (h << 1) + *s;
|
|
|
|
if (h < 0)
|
|
h = -h;
|
|
|
|
return h;
|
|
}
|