add function: fast_allocator_avail_memory

pull/37/merge
YuQing 2021-08-17 10:01:42 +08:00
parent b7ecd0d4c4
commit c6b2c32fe2
3 changed files with 14 additions and 14 deletions

View File

@ -1,5 +1,5 @@
Version 1.54 2021-08-13 Version 1.54 2021-08-17
* fast_allocator.[hc]: correct reclaim_interval logic * fast_allocator.[hc]: correct reclaim_interval logic
* shared_func.[hc]: add functions getFileContentEx1 and getFileContent1 * shared_func.[hc]: add functions getFileContentEx1 and getFileContent1
* fc_queue.[hc]: add function fc_queue_timedpeek * fc_queue.[hc]: add function fc_queue_timedpeek

View File

@ -62,7 +62,7 @@ struct fast_allocator_context
struct fast_allocator_array allocator_array; struct fast_allocator_array allocator_array;
int64_t alloc_bytes_limit; //mater mark bytes for alloc int64_t alloc_bytes_limit; //water mark bytes for alloc
volatile int64_t alloc_bytes; //total alloc bytes volatile int64_t alloc_bytes; //total alloc bytes
bool need_lock; //if need mutex lock for acontext bool need_lock; //if need mutex lock for acontext
}; };
@ -178,9 +178,19 @@ static inline int fast_allocator_alloc_string(struct fast_allocator_context
return fast_allocator_alloc_string_ex(acontext, dest, src->str, src->len); return fast_allocator_alloc_string_ex(acontext, dest, src->str, src->len);
} }
static inline int64_t fast_allocator_avail_memory(
struct fast_allocator_context *acontext)
{
if (acontext->alloc_bytes_limit == 0) {
return INT64_MIN;
}
return acontext->alloc_bytes_limit - __sync_add_and_fetch(
&acontext->allocator_array.malloc_bytes, 0);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -1254,17 +1254,7 @@ int getFileContentEx1(int fd, const char *filename, char *buff,
int result; int result;
int read_bytes; int read_bytes;
if (lseek(fd, offset, SEEK_SET) < 0) { if ((read_bytes=pread(fd, buff, *size - 1, offset)) < 0) {
result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, "
"lseek file %s fail, offset: %"PRId64", "
"errno: %d, error info: %s", __LINE__,
filename, offset, result, STRERROR(result));
*size = 0;
return result;
}
if ((read_bytes=read(fd, buff, *size - 1)) < 0) {
result = errno != 0 ? errno : EIO; result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"read from file %s fail, offset: %"PRId64", " "read from file %s fail, offset: %"PRId64", "