From c6b2c32fe281da29b2640f1e1053a2a76dc4c61a Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Tue, 17 Aug 2021 10:01:42 +0800 Subject: [PATCH] add function: fast_allocator_avail_memory --- HISTORY | 2 +- src/fast_allocator.h | 14 ++++++++++++-- src/shared_func.c | 12 +----------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/HISTORY b/HISTORY index b4de744..2033768 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.54 2021-08-13 +Version 1.54 2021-08-17 * fast_allocator.[hc]: correct reclaim_interval logic * shared_func.[hc]: add functions getFileContentEx1 and getFileContent1 * fc_queue.[hc]: add function fc_queue_timedpeek diff --git a/src/fast_allocator.h b/src/fast_allocator.h index 3627bc3..57fa987 100644 --- a/src/fast_allocator.h +++ b/src/fast_allocator.h @@ -62,7 +62,7 @@ struct fast_allocator_context 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 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); } +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 } #endif #endif - diff --git a/src/shared_func.c b/src/shared_func.c index 39a063c..ff2449b 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1254,17 +1254,7 @@ int getFileContentEx1(int fd, const char *filename, char *buff, int result; int read_bytes; - if (lseek(fd, offset, SEEK_SET) < 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) { + if ((read_bytes=pread(fd, buff, *size - 1, offset)) < 0) { result = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " "read from file %s fail, offset: %"PRId64", "