mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
The goal was to improve readability, but along the way a few things were gained. - Sorted sym and map files - Infrastructure for supporting multiple .o versions - Valgrind-proof, as far as my testing goes anyways - Improved verbosity messages - Added error checking - Performance improvements, see end of commit message The readability improvement was spurred while trying to make sense of the old code while trying to implement features such as sorted sym and map files. I also did my best to remove hardcoded logic, such that modifications should be doable; for example, "RAM loading" sections, which are linked against a different location than the one they're stored at. Some work remains to be done, see the "TODO:" and "FIXME:" comments. Further, while regression tests pass, this new linker should be tested on different codebases (ideally while instrumented with `make develop` and under valgrind). The few errors spotted in the man pages (alignment) need to be corrected. Finally, documentation comments need to be written, I have written a lot of them but not all. This also provides a significant performance boost (benchmarked with a 51994-symbol project): Current master RGBLINK: 2.02user 0.03system 0:02.06elapsed 99%CPU (0avgtext+0avgdata 84336maxresident)k 0inputs+11584outputs (0major+20729minor)pagefaults 0swaps Rewritten RGBLINK: 0.19user 0.06system 0:00.63elapsed 40%CPU (0avgtext+0avgdata 32460maxresident)k 23784inputs+11576outputs (0major+7672minor)pagefaults 0swaps
69 lines
2.3 KiB
C
69 lines
2.3 KiB
C
/*
|
|
* This file is part of RGBDS.
|
|
*
|
|
* Copyright (c) 1997-2019, Carsten Sorensen and RGBDS contributors.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
/* Generic hashmap implementation (C++ templates are calling...) */
|
|
#ifndef RGBDS_LINK_HASHMAP_H
|
|
#define RGBDS_LINK_HASHMAP_H
|
|
|
|
#include <assert.h>
|
|
|
|
#define HASH_NB_BITS 32
|
|
#define HALF_HASH_NB_BITS 16
|
|
_Static_assert(HALF_HASH_NB_BITS * 2 == HASH_NB_BITS, "");
|
|
#define HASHMAP_NB_BUCKETS (1 << HALF_HASH_NB_BITS)
|
|
|
|
/* HashMapEntry is internal, please do not attempt to use it */
|
|
typedef struct HashMapEntry *HashMap[HASHMAP_NB_BUCKETS];
|
|
|
|
/**
|
|
* Adds an element to a hashmap.
|
|
* @warning Adding a new element with an already-present key will not cause an
|
|
* error, this must be handled externally.
|
|
* @warning Inserting a NULL will make `hash_GetElement`'s return ambiguous!
|
|
* @param map The HashMap to add the element to
|
|
* @param key The key with which the element will be stored and retrieved
|
|
* @param element The element to add
|
|
* @return True if a collision occurred (for statistics)
|
|
*/
|
|
bool hash_AddElement(HashMap map, char const *key, void *element);
|
|
|
|
/**
|
|
* Removes an element from a hashmap.
|
|
* @param map The HashMap to remove the element from
|
|
* @param key The key to search the element with
|
|
* @return True if the element was found and removed
|
|
*/
|
|
bool hash_RemoveElement(HashMap map, char const *key);
|
|
|
|
/**
|
|
* Finds an element in a hashmap.
|
|
* @param map The map to consider the elements of
|
|
* @param key The key to search an element for
|
|
* @return A pointer to the element, or NULL if not found. (NULL can be returned
|
|
* if such an element was added, but that sounds pretty silly.)
|
|
*/
|
|
void *hash_GetElement(HashMap const map, char const *key);
|
|
|
|
/**
|
|
* Executes a function on each element in a hashmap.
|
|
* @param map The map to consider the elements of
|
|
* @param func The function to run. The first argument will be the element,
|
|
* the second will be `arg`.
|
|
* @param arg An argument to be passed to all function calls
|
|
*/
|
|
void hash_ForEach(HashMap const map, void (*func)(void *, void *), void *arg);
|
|
|
|
/**
|
|
* Cleanly empties a hashmap from its contents.
|
|
* This does not `free` the data structure itself!
|
|
* @param map The map to empty
|
|
*/
|
|
void hash_EmptyMap(HashMap map);
|
|
|
|
#endif /* RGBDS_LINK_HASHMAP_H */
|