aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/browser_asset_cache.c (4369B)


      1 #include "browser_asset_cache.h"
      2 #include "browser_profile.h"
      3 #include <errno.h>
      4 #include <pthread.h>
      5 #include <stdint.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <unistd.h>
     10 #ifdef __EMSCRIPTEN__
     11 #include <emscripten.h>
     12 EMSCRIPTEN_KEEPALIVE void browser_store_startup_chunk(uint32_t inode, uint32_t index, unsigned char *bytes)
     13 {
     14     browser_asset_cache_store(inode, (off_t)index * 1048576, bytes, 1048576);
     15 }
     16 #endif
     17 
     18 static struct asset_chunk {
     19     ino_t inode;
     20     off_t offset;
     21     size_t size;
     22     unsigned char *bytes;
     23     struct asset_chunk *next;
     24 } *chunks;
     25 static size_t cached_bytes;
     26 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     27 
     28 /* Publish complete chunks only. A demand read may have won the same race. */
     29 void browser_asset_cache_store(ino_t inode, off_t offset, unsigned char *bytes, size_t size)
     30 {
     31     pthread_mutex_lock(&mutex);
     32     for (struct asset_chunk *chunk = chunks; chunk; chunk = chunk->next) {
     33         if (chunk->inode == inode && chunk->offset == offset) {
     34             pthread_mutex_unlock(&mutex);
     35             free(bytes);
     36             return;
     37         }
     38     }
     39     while (cached_bytes + size > 256 * 1024 * 1024) {
     40         struct asset_chunk **oldest = &chunks;
     41         while ((*oldest)->next) oldest = &(*oldest)->next;
     42         cached_bytes -= (*oldest)->size;
     43         free((*oldest)->bytes);
     44         free(*oldest);
     45         *oldest = NULL;
     46     }
     47     struct asset_chunk *chunk = malloc(sizeof(*chunk));
     48     if (!chunk) abort();
     49     *chunk = (struct asset_chunk){.inode = inode, .offset = offset, .size = size,
     50         .bytes = bytes, .next = chunks};
     51     chunks = chunk;
     52     cached_bytes += size;
     53     pthread_mutex_unlock(&mutex);
     54 }
     55 
     56 ssize_t browser_asset_read_locked(int descriptor, ino_t inode, off_t file_size,
     57                                  void *buffer, size_t length)
     58 {
     59     off_t position = lseek(descriptor, 0, SEEK_CUR);
     60     if (position < 0) return -1;
     61     if (position >= file_size || !length) return 0;
     62     if (length > (uint64_t)(file_size - position)) length = file_size - position;
     63     size_t copied = 0;
     64     int filled = 0;
     65 
     66     while (copied < length) {
     67         off_t offset = (position + copied) / 1048576 * 1048576;
     68         pthread_mutex_lock(&mutex);
     69         struct asset_chunk **entry = &chunks;
     70         while (*entry && ((*entry)->inode != inode || (*entry)->offset != offset))
     71             entry = &(*entry)->next;
     72         struct asset_chunk *chunk = *entry;
     73         double hit = chunk && !filled ? ao_profile_begin("asset cache hit") : 0;
     74 
     75         if (chunk) {
     76             /* Move resident chunks to the front without contacting the fetch worker. */
     77             *entry = chunk->next;
     78         } else {
     79             size_t size = file_size - offset < 1048576 ? file_size - offset : 1048576;
     80             pthread_mutex_unlock(&mutex);
     81 
     82             /* One worker request fills a complete chunk for subsequent small reads. */
     83             unsigned char *bytes = malloc(size);
     84             if (!bytes) abort();
     85             double start = ao_profile_begin("asset cache fill");
     86             ssize_t result;
     87             do {
     88                 result = pread(descriptor, bytes, size, offset);
     89             } while (result < 0 && errno == EINTR);
     90             if (result == (ssize_t)size) browser_asset_cache_store(inode, offset, bytes, size);
     91             char detail[64] = "";
     92             pthread_mutex_lock(&mutex);
     93             if (start) snprintf(detail, sizeof(detail), "resident_bytes=%zu", cached_bytes);
     94             pthread_mutex_unlock(&mutex);
     95             ao_profile_end("asset cache fill", start, detail);
     96             if (result != (ssize_t)size) {
     97                 free(bytes);
     98                 if (result >= 0) errno = EIO;
     99                 if (!copied) return -1;
    100                 break;
    101             }
    102             filled = 1;
    103             continue;
    104         }
    105         chunk->next = chunks;
    106         chunks = chunk;
    107 
    108         size_t within = position + copied - offset;
    109         size_t size = chunk->size - within;
    110         if (size > length - copied) size = length - copied;
    111         memcpy((unsigned char *)buffer + copied, chunk->bytes + within, size);
    112         copied += size;
    113         filled = 0;
    114         pthread_mutex_unlock(&mutex);
    115         if (hit) ao_profile_end("asset cache hit", hit, NULL);
    116     }
    117     if (lseek(descriptor, position + copied, SEEK_SET) < 0) return -1;
    118     return copied;
    119 }