comparison dowa/d_memory.c @ 87:d39e8860a361

[Dowa] There was alignment issues rea hash key
author June Park <parkjune1995@gmail.com>
date Thu, 01 Jan 2026 14:25:17 -0800
parents 4532ce6d9eb8
children 655ea0b661fd
comparison
equal deleted inserted replaced
86:431df06b1a9b 87:d39e8860a361
107 else 107 else
108 { 108 {
109 current_capacity = 0; 109 current_capacity = 0;
110 } 110 }
111 111
112 if (current_capacity >= minimum_capacity && p_array != NULL) 112 // Calculate needed capacity: if minimum_capacity is 0, we need room for at least one more element
113 size_t needed_capacity = minimum_capacity;
114 if (p_array && needed_capacity == 0)
115 needed_capacity = p_header->length + 1;
116
117 if (current_capacity >= needed_capacity && p_array != NULL)
113 return p_array; 118 return p_array;
114 119
115 new_capacity = current_capacity * 2; 120 new_capacity = current_capacity * 2;
116 if (new_capacity < 4) 121 if (new_capacity < 4)
117 new_capacity = 4; 122 new_capacity = 4;
118 if (new_capacity < minimum_capacity) 123 if (new_capacity < needed_capacity)
119 new_capacity = minimum_capacity; 124 new_capacity = needed_capacity;
120 125
121 size_t total_size = sizeof(Dowa_Array_Header) + (element_size * new_capacity); 126 size_t total_size = sizeof(Dowa_Array_Header) + (element_size * new_capacity);
122 127
123 if (p_arena) 128 if (p_arena)
124 { 129 {
477 if (!p_key_copy) 482 if (!p_key_copy)
478 return p_map; 483 return p_map;
479 484
480 memcpy(p_key_copy, p_key, key_size); 485 memcpy(p_key_copy, p_key, key_size);
481 486
482 memcpy(p_new_element, &p_key_copy, sizeof(char*)); 487 // Store the pointer value (use memcpy to avoid alignment issues)
488 memcpy(p_new_element, &p_key_copy, sizeof(p_key_copy));
483 p_header->length++; 489 p_header->length++;
484 490
485 // Try to insert into hash table - if it fails, rehash and retry 491 // Try to insert into hash table - if it fails, rehash and retry
486 boolean inserted = FALSE; 492 boolean inserted = FALSE;
487 for (int attempt = 0; attempt < 2 && !inserted; attempt++) 493 for (int attempt = 0; attempt < 2 && !inserted; attempt++)
495 p_header = dowa__header(p_map); 501 p_header = dowa__header(p_map);
496 502
497 // Restore array state 503 // Restore array state
498 new_index = (uint32)p_header->length; 504 new_index = (uint32)p_header->length;
499 p_new_element = (char*)p_map + (new_index * element_size); 505 p_new_element = (char*)p_map + (new_index * element_size);
500 memcpy(p_new_element, &p_key_copy, sizeof(char*)); 506 memcpy(p_new_element, &p_key_copy, sizeof(p_key_copy));
501 p_header->length++; 507 p_header->length++;
502 } 508 }
503 509
504 size_t bucket_mask = p_index->bucket_count - 1; 510 size_t bucket_mask = p_index->bucket_count - 1;
505 size_t bucket_index = hash & bucket_mask; 511 size_t bucket_index = hash & bucket_mask;