-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (41 loc) · 1.07 KB
/
Copy pathmain.cpp
File metadata and controls
46 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Created by Administrator on 25-4-8.
//
#include "MemoryPool.h"
#include<string.h>
struct Data {
char* ptr;
FILE * pfile;
};
void func1(void *p) {
auto p1 =static_cast<char *>(p);
std::cout<<"free ptr mem"<<std::endl;
free(p1);
}
void func2(void *p) {
auto p1 =static_cast<FILE *>(p);
std::cout<<"close file"<<std::endl;
fclose(p1);
}
int main() {
MemoryPool ngx_pool(512);
void *p1 = ngx_pool.ngx_palloc(128);
if (!p1) {
std::cout<<"ngx_palloc 128 bytes fail"<<std::endl;
}
Data *p2 =static_cast<Data *>(ngx_pool.ngx_palloc(512));
if (!p2) {
std::cout<<"ngx_palloc 512 bytes fail"<<std::endl;
}
p2->ptr = static_cast<char *>(malloc(12));
strcpy(p2->ptr,"hello world");
p2->pfile=fopen("data.txt","w");
ngx_pool_cleanup_t *c1 = ngx_pool.ngx_pool_cleanup_add(sizeof(char*));
c1->handler = func1;
c1->data = p2->ptr;
ngx_pool_cleanup_t *c2 = ngx_pool.ngx_pool_cleanup_add(sizeof(FILE*));
c2->handler = func2;
c2->data = p2->pfile;
ngx_pool.ngx_destroy_pool();
return 0;
}