int CpfsWrite(const char* buf, uint32_t size, off_t off, CpfsFilesystem* fs, const CpfsFileHandle* handle, void (*callback)(int, void*), void* data) { if (callback) { CLIENT_PTR(fs)->fs_async_rw()->Add(new cpfs::client::APIWriteRequest( buf, size, off, handle, callback, data)); return 0; } else { return CLIENT_PTR(fs)->api_common()->Write(buf, size, off, handle); } }
/*----------------------------------------------------------------------------- * malloc, calloc, strdup *----------------------------------------------------------------------------*/ static void *m_alloc( size_t size, int fill, char *source, char *file, int lineno ) { MemBlock *block; void *memptr; init_module(); block = new_block( size, file, lineno ); check_mem( block ); memptr = CLIENT_PTR( block ); if ( fill >= 0 ) { /* calloc */ memset( memptr, fill, size ); } else if ( source ) { /* strdup */ strcpy( (char *)memptr, source ); } else { /* malloc */ } return memptr; error: return NULL; }
DEFINE_dtor_module() { MemBlock *block, *block_count; void *memptr; int count1, count2; /* delete all existing blocks in reverse order */ while ( (block = next_to_free()) != NULL ) { DL_COUNT(g_mem_blocks, block_count, count1); /* skip memory leak warning if declared to destroy at exit */ if ( ! block->flags.destroy_atextit ) log_warn("memory leak (%u bytes) allocated at %s:%d", block->client_size, block->file ? block->file : "(null)", block->lineno ); /* delete from g_mem_blocks */ memptr = CLIENT_PTR(block); m_free( memptr ); /* assert that block was freed */ DL_COUNT(g_mem_blocks, block_count, count2); check(count2 < count1, "block not freed"); } error: ; }
/*----------------------------------------------------------------------------- * realloc *----------------------------------------------------------------------------*/ void *m_realloc_( void *memptr, size_t size, char *file, int lineno ) { MemBlock *block, *next_block; Bool result; init_module(); /* if input is NULL, behave as malloc */ if ( memptr == NULL ) return m_malloc_( size, file, lineno ); /* find the block */ block = find_block( memptr, file, lineno ); check( block, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* delete from list as realloc may move block */ next_block = block->next; /* remember position */ DL_DELETE(g_mem_blocks, block); /* check fences */ result = check_fences( block ); check( result, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* reallocate and create new end fence */ block = realloc( block, BLOCK_SIZE( size ) ); check( block, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* update block */ block->client_size = size; block->file = file; block->lineno = lineno; /* fill end fence */ memset( END_FENCE_PTR( block ), FENCE_SIGN, FENCE_SIZE ); /* add to list at the same location as before */ if (next_block == NULL) DL_APPEND(g_mem_blocks, block); else DL_PREPEND_ELEM(g_mem_blocks, next_block, block); return CLIENT_PTR( block ); error: return NULL; }
int CpfsGetattr(struct stat* stbuf, CpfsFilesystem* fs, CpfsFileHandle* handle) { return CLIENT_PTR(fs)->api_common()->Getattr(stbuf, handle); }
int CpfsClose(CpfsFilesystem* fs, CpfsFileHandle* handle) { return CLIENT_PTR(fs)->api_common()->Close(handle); }
int CpfsOpen(const char* path, int flags, mode_t mode, CpfsFilesystem* fs, CpfsFileHandle* handle) { return CLIENT_PTR(fs)->api_common()->Open(path, flags, mode, handle); }
void CpfsShutdown(CpfsFilesystem* fs) { CLIENT_PTR(fs)->Shutdown(); delete CLIENT_PTR(fs); }
int CpfsSetattr(struct stat* stbuf, int to_set, CpfsFilesystem* fs, CpfsFileHandle* handle) { return CLIENT_PTR(fs)->api_common()->Setattr(stbuf, to_set, handle); }