void write_file(int inum, FILE *inf, int size, int offset) { resize_file(inum, offset); u8 bytes[size]; for (int i = 0; i < size; i++) { u8 byte = fgetc(inf); bytes[i] = byte; } append_data_to_file(inum, bytes, size); }
void write_data_to_file(int inum, void *data, size_t size, int offset) { resize_file(inum, offset); append_data_to_file(inum, data, size); }
static int am_shm_extend(am_shm_t *am, size_t usize) { size_t size, osize; int rv = AM_SUCCESS; if (usize == 0 || am == NULL || am->pool == NULL) { return AM_EINVAL; } size = page_size(usize + SIZEOF_mem_pool); #ifdef _WIN32 if (UnmapViewOfFile(am->pool) == 0) { am->error = GetLastError(); return AM_ERROR; } if (CloseHandle(am->h[2]) == 0) { am->error = GetLastError(); return AM_ERROR; } if (resize_file(am->h[1], size) == FALSE) { return AM_ERROR; } am->h[2] = CreateFileMappingA(am->h[1], NULL, PAGE_READWRITE, 0, (DWORD) size, NULL); am->error = GetLastError(); if (am->h[2] == NULL) { return AM_ERROR; } am->pool = (struct mem_pool *) MapViewOfFile(am->h[2], FILE_MAP_ALL_ACCESS, 0, 0, 0); am->error = GetLastError(); if (am->pool == NULL || (am->error != 0 && am->error != ERROR_ALREADY_EXISTS)) { rv = AM_ERROR; } else #else osize = ((struct mem_pool *) am->pool)->size; rv = ftruncate(am->fd, size); if (rv == -1) { am->error = errno; return AM_EINVAL; } munmap(am->pool, osize); am->pool = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, am->fd, 0); if (am->pool == MAP_FAILED) { am->error = errno; rv = AM_ERROR; } else #endif { struct mem_pool *pool = (struct mem_pool *) am->pool; struct mem_chunk *last = (struct mem_chunk *) AM_GET_POINTER(pool, pool->lh.next); if (last == NULL) { am->error = AM_ENOMEM; return AM_ERROR; } if (last->used == 0) { /* the last chunk is not used - add all newly allocated space there */ last->size += size - pool->size; } else { /* the last chunk is used - add all newly allocated space right after the last chunk * adjusting both - next pointer of the last chunk and head node to point to it */ struct mem_chunk *e = (struct mem_chunk *) ((char *) pool + pool->size); e->used = 0; e->usize = 0; e->size = size - pool->size; e->lh.prev = AM_GET_OFFSET(pool, last); e->lh.next = 0; pool->lh.next = last->lh.next = AM_GET_OFFSET(pool, e); } *(am->global_size) = am->local_size = pool->size = size; /* new size */ am->error = AM_SUCCESS; } return rv; }