ssize_t mf_read(mf_handle_t mf, void* buf, size_t count, off_t offset) { if (count <= 0 || offset < 0 || buf == NULL || mf == NULL) { errno = EINVAL; return 0; } file_handle_t* fh = mf; if (fh->file_size < 0) return 0; if (offset > fh->file_size) { errno = EINVAL; return 0; } void* addr = NULL; chunk_t* chunk; chunk_find(mf, &chunk, &addr, offset, count); chunk->ref_count -= 1; if ((offset + count) > fh->file_size) { memcpy(buf, addr, fh->file_size - offset); return fh->file_size - offset; } else memcpy(buf, addr, count); return count; }
void *mf_map(mf_handle_t mf, off_t offset, size_t size, mf_mapmem_handle_t *mapmem_handle) { if (mf == MF_OPEN_FAILED || offset < 0 || size <= 0 || mapmem_handle == NULL) { *mapmem_handle = NULL; return MF_MAP_FAILED; } file_handle_t* fh = mf; if (offset > fh->file_size) { *mapmem_handle = NULL; return MF_MAP_FAILED; } chunk_t** map_chunk = (chunk_t**)mapmem_handle; chunk_t* chunk = NULL; void* addr = NULL; if (!chunk_find(mf, &chunk, &addr, offset, size)) *mapmem_handle = chunk; else { *mapmem_handle = NULL; return MF_MAP_FAILED; } return addr; }
ssize_t mf_write(mf_handle_t mf, const void* buf, size_t count, off_t offset) { if (count <= 0 || offset < 0 || buf == NULL || mf == NULL) return 0; file_handle_t* fh = mf; sem_wait(&(fh->lock_map)); if (fh->file_size < 0) { sem_post(&(fh->lock_map)); return 0; } if (offset > fh->file_size || (offset + count) > fh->file_size) { errno = EINVAL; sem_post(&(fh->lock_map)); return 0; } void* addr = NULL; chunk_t* chunk; chunk_find(mf, &chunk, &addr, offset, count); chunk->ref_count -=1; memcpy(addr, buf, count); sem_post(&(fh->lock_map)); return count; }
void *malloc_(size_t size) { if (size == 0) return (NULL); if (g_malloc_memory == NULL) g_malloc_memory = new_(sizeof(t_chunk)); return (chunk_find(size)); }