| 3 | XRCORE_API smem_container* g_pSharedMemoryContainer = NULL; |
| 4 | |
| 5 | smem_value* smem_container::dock(u32 dwSize, void* ptr) |
| 6 | { |
| 7 | VERIFY(dwSize && ptr); |
| 8 | |
| 9 | if (bDisable) |
| 10 | { |
| 11 | smem_value* result = (smem_value*)xr_malloc(sizeof(smem_value) + dwSize); |
| 12 | result->dwReference = 0; |
| 13 | result->dwSize = dwSize; |
| 14 | result->dwCRC = u32(-1); |
| 15 | CopyMemory(result->value, ptr, dwSize); |
| 16 | |
| 17 | return result; |
| 18 | } |
| 19 | |
| 20 | cs.Enter(); |
| 21 | smem_value* result = 0; |
| 22 | |
| 23 | u32 dwCRC = crc32(ptr, dwSize); |
| 24 | |
| 25 | // search a place to insert |
| 26 | u8 storage[sizeof(smem_value)]; |
| 27 | smem_value* value = (smem_value*)storage; |
| 28 | value->dwReference = 0; |
| 29 | value->dwCRC = dwCRC; |
| 30 | value->dwSize = dwSize; |
| 31 | cdb::iterator it = std::lower_bound(container.begin(), container.end(), value, smem_search); |
| 32 | cdb::iterator saved_place = it; |
| 33 | if (container.end() != it) |
| 34 | { |
| 35 | // supposedly found |
| 36 | for (;; it++) |
| 37 | { |
| 38 | if (it == container.end()) |
| 39 | break; |
| 40 | if ((*it)->dwCRC != dwCRC) |
| 41 | break; |
| 42 | if ((*it)->dwSize != dwSize) |
| 43 | break; |
| 44 | if (0 == memcmp((*it)->value, ptr, dwSize)) |
| 45 | { |
| 46 | // really found |
| 47 | result = *it; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // if not found - create new entry |
| 54 | if (0 == result) |
| 55 | { |
| 56 | result = (smem_value*)xr_malloc(sizeof(smem_value) + dwSize); |
| 57 | result->dwReference = 0; |
| 58 | result->dwCRC = dwCRC; |
| 59 | result->dwSize = dwSize; |
| 60 | CopyMemory(result->value, ptr, dwSize); |
| 61 | container.insert(saved_place, result); |
| 62 | } |