cache.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "cache.h"
  2. #include "material.h"
  3. #include "texture.h"
  4. #define AHASH_IMPLEMENT
  5. #include "ahash.h"
  6. #include "mempools.h"
  7. static struct {
  8. AHash materials;
  9. AHash textures;
  10. } g;
  11. static void initHash(AHash *hash, struct Stack *pool, long item_size) {
  12. hash->alloc_param = pool;
  13. hash->alloc = (AHashAllocFunc)stackAlloc;
  14. hash->nbuckets = 64;
  15. hash->key_size = 256;
  16. hash->value_size = item_size;
  17. hash->key_hash = aHashStringHash;
  18. hash->key_compare = (AHashKeyCompareFunc)strcmp;
  19. aHashInit(hash);
  20. }
  21. void cacheInit(struct Stack *pool) {
  22. initHash(&g.materials, pool, sizeof(struct Material));
  23. initHash(&g.textures, pool, sizeof(struct Texture));
  24. }
  25. const struct Material *cacheGetMaterial(const char *name) {
  26. return aHashGet(&g.materials, name);
  27. }
  28. void cachePutMaterial(const char *name, const struct Material *mat /* copied */) {
  29. aHashInsert(&g.materials, name, mat);
  30. }
  31. const struct Texture *cacheGetTexture(const char *name) {
  32. return aHashGet(&g.textures, name);
  33. }
  34. void cachePutTexture(const char *name, const struct Texture *mat /* copied */) {
  35. aHashInsert(&g.textures, name, mat);
  36. }