Mercurial
annotate dowa/d_memory.c @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 0a9e67c7039a |
| children | 947b81010aba |
| rev | line source |
|---|---|
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "dowa.h" |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
3 // --- Arena --- // |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
4 Dowa_PArena Dowa_Arena_Create(size_t capacity) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
5 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
6 Dowa_PArena p_arena = malloc(sizeof(Dowa_Arena)); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
7 if (p_arena == NULL) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
8 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
9 perror("malloc"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
10 return NULL; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
11 } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
12 p_arena->buffer = malloc(capacity); |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
13 p_arena->offset = 0; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
14 p_arena->capacity = capacity; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
15 return p_arena; |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
16 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
17 |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
18 void *Dowa_Arena_Allocate(Dowa_PArena p_arena, size_t size) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
19 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
20 if (p_arena->offset + size > p_arena->capacity) |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
21 { |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
22 return NULL; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
23 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
24 void *currnet_ptr = p_arena->buffer + p_arena->offset; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
25 p_arena->offset += size; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
26 return currnet_ptr; |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
27 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
28 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
29 void Dowa_Arena_Destroy(Dowa_PArena p_arena) |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
30 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
31 if (p_arena) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
32 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
33 if (p_arena->buffer) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
34 free(p_arena->buffer); |
|
1
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
35 free(p_arena); |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
36 } |
|
adcfad6e86fb
Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
37 } |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
38 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
39 // --- HashMap --- // |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
40 Dowa_PHashMap Dowa_HashMap_Create(size_t capacity) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
41 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
42 Dowa_PHashMap p_hash_map; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
43 p_hash_map = malloc(sizeof(Dowa_HashMap)); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
44 if (p_hash_map == NULL) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
45 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
46 return NULL; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
47 } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
48 p_hash_map->entries = calloc(capacity, sizeof(*p_hash_map->entries)); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
49 if (p_hash_map->entries == NULL) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
50 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
51 free(p_hash_map); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
52 return NULL; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
53 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
54 p_hash_map->capacity = capacity; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
55 p_hash_map->current_capacity = 0; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
56 return p_hash_map; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
57 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
58 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
59 Dowa_PHashMap Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_PArena p_arena) |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
60 { |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
61 if (p_arena == NULL) |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
62 printf("Arena is NULL\n"); |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
63 return Dowa_HashMap_Create(capacity); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
64 |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
65 Dowa_PHashMap p_hash_map; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
66 p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap)); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
67 if (p_hash_map == NULL) |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
68 { |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
69 return NULL; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
70 } |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
71 p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
72 if (p_hash_map->entries == NULL) |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
73 { |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
74 free(p_hash_map); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
75 return NULL; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
76 } |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
77 p_hash_map->capacity = capacity; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
78 p_hash_map->current_capacity = 0; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
79 p_hash_map->p_arena = p_arena; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
80 return p_hash_map; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
81 } |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
82 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
83 void Dowa_HashMap_Destroy(Dowa_PHashMap p_hash_map) |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
84 { |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
85 if (!p_hash_map) return; |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
86 |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
87 if (p_hash_map->p_arena) |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
88 Dowa_Arena_Destroy(p_hash_map->p_arena); |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
89 else |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
90 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
91 Dowa_PHashEntry entry; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
92 if (p_hash_map->entries) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
93 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
94 for (int idx=0; idx<p_hash_map->capacity; idx++) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
95 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
96 entry = p_hash_map->entries[idx]; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
97 if (entry) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
98 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
99 free(entry->key); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
100 free(entry->buffer); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
101 free(entry); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
102 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
103 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
104 } |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
105 free(p_hash_map->entries); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
106 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
107 free(p_hash_map); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
108 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
109 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
110 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key) |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
111 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
112 int32 hash_val = HASH_KEY_NUMBER; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
113 int32 c; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
114 while ((c = *key++)) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
115 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
116 hash_val = (hash_val << 5) + hash_val + c; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
117 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
118 return hash_val % p_hash_map->capacity; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
119 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
120 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
121 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, const char *key) |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
122 { |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
123 int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
124 void *value = p_hash_map->entries[idx_foo]; |
|
18
fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents:
6
diff
changeset
|
125 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
126 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
127 return NULL; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
128 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
129 return ((Dowa_PHashEntry) value)->buffer; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
130 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
131 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
132 int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, const char *key, |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
133 void *value, size_t value_size, |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
134 Dowa_HashMap_ValueType type) |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
135 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
136 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
137 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
138 if (entry) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
139 free(entry->buffer); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
140 else |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
141 { |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
142 entry = p_hash_map->p_arena ? |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
143 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
144 malloc(sizeof(Dowa_HashEntry)); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
145 if (entry == NULL) { perror("malloc or arena alloc"); return -1; } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
146 |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
147 p_hash_map->entries[idx] = entry; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
148 p_hash_map->current_capacity++; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
149 entry->key = strdup(key); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
150 } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
151 entry->buffer = value; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
152 entry->capacity = value_size; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
153 entry->type = type; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
154 return 0; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
155 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
156 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
157 int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, const char *key, |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
158 void *value, size_t value_size, |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
159 Dowa_HashMap_ValueType type) |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
160 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
161 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
162 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
163 if (entry) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
164 free(entry->buffer); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
165 else |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
166 { |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
167 entry = p_hash_map->p_arena ? |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
168 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
169 malloc(sizeof(Dowa_HashEntry)); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
170 if (entry == NULL) { perror("malloc or arena alloc"); return -1; } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
171 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
172 p_hash_map->entries[idx] = entry; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
173 p_hash_map->current_capacity++; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
174 entry->key = strdup(key); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
175 } |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
176 entry->buffer = p_hash_map->p_arena ? |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
177 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
178 malloc(value_size); |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
179 |
|
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
180 if (entry->buffer == NULL) { perror("malloc or arena alloc"); return -1; } |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
181 entry->capacity = value_size; |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
182 entry->type = type; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
183 memcpy(entry->buffer, value, value_size); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
184 return 0; |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
185 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
186 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
187 void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
188 { |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
189 Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
190 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
191 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
192 void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, const char *key) |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
193 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
194 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
195 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
196 if (entry) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
197 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
198 free(entry->key); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
199 free(entry->buffer); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
200 free(entry); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
201 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
202 p_hash_map->entries[idx] = NULL; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
203 if (p_hash_map->current_capacity > 0) |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
204 { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
205 p_hash_map->current_capacity--; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
206 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
207 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
1
diff
changeset
|
208 |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
209 void Dowa_HashMap_Print(Dowa_PHashMap map) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
210 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
211 printf("\n-----------\n\n"); |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
212 for (size_t i = 0; i < map->capacity; ++i) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
213 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
214 Dowa_PHashEntry e = map->entries[i]; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
215 if (!e) continue; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
216 printf("%s: ", e->key); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
217 switch (e->type) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
218 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
219 case DOWA_HASH_MAP_TYPE_BUFFER: |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
220 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
221 unsigned char *p = e->buffer; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
222 for (size_t j = 0; j < e->capacity; ++j) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
223 { |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
224 printf("%02x", p[j]); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
225 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
226 printf("\n"); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
227 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
228 break; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
229 case DOWA_HASH_MAP_TYPE_STRING: |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
230 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
231 printf("%s\n", (char*)e->buffer); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
232 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
233 break; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
234 case DOWA_HASH_MAP_TYPE_HASHMAP: |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
235 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
236 printf("This is a hashmap with size of %zu, and pointer %p\n", (Dowa_PHashMap)e->capacity, (void *)e); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
237 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
238 break; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
239 case DOWA_HASH_MAP_TYPE_INT: |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
240 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
241 printf("%d\n", *(int32*)e->buffer); |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
242 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
243 break; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
244 default: |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
245 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
246 printf("<unknown type>\n"); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
247 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
248 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
249 } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
250 printf("-----------\n"); |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
251 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
252 |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
253 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
254 { |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
255 DIR *dir = opendir(folder_path); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
256 if (!dir) { perror("opendir"); return -1; } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
257 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
258 struct dirent *entry; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
259 while ((entry = readdir(dir))) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
260 { |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
261 // skip "." and ".." |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
262 if (entry->d_name[0] == '.' && |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
263 (entry->d_name[1] == '\0' || |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
264 (entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
265 continue; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
266 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
267 char fullpath[PATH_MAX]; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
268 snprintf(fullpath, sizeof fullpath, "%s/%s", folder_path, entry->d_name); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
269 |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
270 struct stat st; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
271 if (stat(fullpath, &st) < 0) { perror("stat"); continue; } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
272 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
273 if (S_ISREG(st.st_mode)) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
274 { |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
275 size_t size = (size_t)st.st_size; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
276 FILE *f = fopen(fullpath, "rb"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
277 if (!f) { perror("fopen"); continue; } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
278 |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
279 void *buf = p_hash_map->p_arena ? |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
280 Dowa_Arena_Allocate(p_hash_map->p_arena, size) : |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
281 malloc(size); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
282 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
283 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
284 if (fread(buf, 1, size, f) != size) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
285 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
286 perror("fread"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
287 free(buf); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
288 fclose(f); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
289 continue; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
290 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
291 fclose(f); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
292 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
293 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
294 free(buf); // Dowa_HashMap_PushValue made its own copy |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
295 } |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
296 else if (S_ISDIR(st.st_mode)) |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
297 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
298 Dowa_PHashMap p_child_map = Dowa_HashMap_Create_With_Arena(100, p_hash_map->p_arena); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
299 if (!p_child_map) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
300 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
301 perror("Dowa_HashMap_Create"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
302 return -1; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
303 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
304 if (Dowa_HashMap_Cache_Folder(p_child_map, fullpath) == -1) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
305 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
306 perror("Dowa_HashMap_Cache_Folder"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
307 return -1; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
308 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
309 |
|
6
1e61008b9980
[Seobeo] Updated seobeo so that API is more opinionated. Added my webpage./build.sh
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
310 // Should not copy as we malloced already. |
|
20
0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
June Park <parkjune1995@gmail.com>
parents:
18
diff
changeset
|
311 if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map, |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
312 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
313 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
20
diff
changeset
|
314 Dowa_HashMap_Destroy(p_child_map); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
315 return -1; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
316 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
3
diff
changeset
|
317 } |
|
3
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
318 } |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
319 closedir(dir); |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
320 return 0; |
|
2758f5527d2b
[Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
321 } |