- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用必须读取同时连接的数千个套接字客户端的服务器。客户端请求由具有大约32个字节的所有相同确切大小的消息构成。
我正在阅读有关slab allocator
的信息,当我调用read
从套接字中获取数据时,我想在我的应用程序中使用此特殊技术(read
将数据从内核缓冲区复制到我选择的缓冲区中,我想使用一些动态分配的内存)。
在我阅读本文时,似乎Linux内核已经在使用这种技术。如果将其用于实现malloc or new
,鉴于分配已经有效,我仍然值得这样做吗?
我当时在想,通过在没有SLAB算法的情况下在堆栈上使用分配可能会更好,但是我不确定哪种方法是最好的。
最佳答案
如果您是C程序员,那么您当然应该对内存管理有所了解!
但是,除非真正接近机器的极限,否则看起来不可能简单地通过malloc分配每个请求就不会遇到任何问题。但是我相信了解您的替代方案比相信某人的话要好。这里是一些需要考虑的想法。
静态数组
最简单的替代方法是使用单个全局请求槽数组,并跟踪正在使用的请求槽。这意味着静态限制了多少个请求,但是另一方面,这没有开销,也没有碎片的实际问题。只需将限制设置得很高。
这是一个示例实现。如果您不熟悉按位操作,可能会有些困惑,但要点是,我们还有一个额外的数组,每个请求插槽包含一个位(开或关),用于指定该插槽是否正在使用。您可以改为向结构本身添加“is_used”变量,但这最终会使结构填充多于一个位,这不利于我们将开销降至最低的目标。
头文件非常小(顺便说一下,这是C的真正美丽!):
typedef struct request_s {
/* your 32 bytes of information */
unsigned char data[32];
} request_t;
request_t *alloc_request(void);
void free_request(request_t *req);
#include the header file
/* note: this example is not written with multithreading in mind */
/* allow a million requests (total 32 MB + 128 KB of memory) */
#define MAX_REQUESTS (1*1024*1024)
static request_t g_requests[MAX_REQUESTS];
/* use one bit per request to store whether it's in use */
/* unsigned int is 32 bits. shifting right by 5 divides by 32 */
static unsigned int g_requests_used[MAX_REQUESTS >> 5];
request_t *alloc_request(void) {
/* note: this is a very naive method. you really don't want to search
* from the beginning every time, but i'll leave improving that as an
* exercise for you. */
unsigned int word_bits;
unsigned int word, bit;
/* look through the bit array one word (i.e., 32 bits) at a time */
for (word = 0; word < (MAX_REQUESTS >> 5); word++) {
word_bits = g_requests_used[word];
/* we can tell right away whether the entire chunk of 32 requests is
* in use, and avoid the inner loop */
if (word_bits == 0xFFFFFFFFU)
continue;
/* now we know there is a gap somewhere in this chunk, so we loop
* through the 32 bits to find it */
for (bit = 0; bit < 32; bit++) {
if (word_bits & (1U << bit))
continue; /* bit is set, slot is in use */
/* found a free slot */
g_requests_used[word] |= 1U << bit;
return &g_requests[(word << 5) + bit];
}
}
/* we're all out of requests! */
return NULL;
}
void free_request(request_t *req) {
/* make sure the request is actually within the g_requests block of
* memory */
if (req >= g_requests && req < g_requests + MAX_REQUESTS) {
/* find the overall index of this request. pointer arithmetic like this
* is somewhat peculiar to c/c++, you may want to read up on it. */
ptrdiff_t index = req - g_requests;
/* reducing a ptrdiff_t to an unsigned int isn't something you should
* do without thinking about it first. but in our case, we're fine as
* long as we don't allow more than 2 billion requests, not that our
* computer could handle that many anyway */
unsigned int u_index = (unsigned int)index;
/* do some arithmetic to figure out which bit of which word we need to
* turn off */
unsigned int word = u_index >> 5; /* index / 32 */
unsigned int bit = u_index & 31; /* index % 32 */
g_requests_used[word] &= ~(1U << bit);
}
}
index / 32
而不是
index >> 5
,依此类推,编译器会为您进行优化。但这对我来说并不对劲……)
#define REQUESTS_PER_POOL 1024
typedef struct request_pool_s request_pool_t;
struct request_pool_s {
request_t requests[REQUESTS_PER_POOL];
unsigned int requests_used[REQUESTS_PER_POOL >> 5];
request_pool_t *prev;
request_pool_t *next;
};
关于c - 现在实现平板分配器值得吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860047/
我的应用程序中有两个 Activity 。第一个 Activity 启动模式是 singleInstance,第二个 Activity 启动模式是 singleTask。我正在使用这些启动模式,因为我
据热心网友投稿,小米小爱触屏音箱Pro 8外观曝光,可以看到触控屏幕尺寸比较大,像是在音箱上“长”了一个平板。 从曝光的信息来看,小米小爱触屏音箱Pro 8具有白色的配色设计,下方有一个长
我有一张 table ,看起来像, VisitorId date deviceType 1 2018-12-11 mobile 2
今天下午,小米官方公布了小爱触屏音箱Pro 8,可以看到触控屏幕尺寸比较大,音箱上“长”了一个平板。据悉,小米小爱触屏音箱Pro 8具有白色的配色设计,下方有一个长条状的扬声器,上方带有一个尺寸比较
有没有办法检测是否使用手持浏览器(iOS/Android 手机/平板电脑)? 我尝试这样做的目的是让手持设备上的浏览器中的元素宽度减半,但这并没有什么不同。 width: 600px; @media
目前,Google Analytics for web 公开了一个设备类别字段,其离散值为mobile、tablet 和desktop。该界面还允许您更深入地了解它的具体设备。我想 Google 有某
我是一名优秀的程序员,十分优秀!