Line data Source code
1 48 : MAILPATH* pathpool_get(LOGGER log, PATHPOOL* pool){
2 : unsigned i;
3 :
4 48 : if(!pool || (!pool->paths && pool->count != 0)){
5 0 : return NULL;
6 : }
7 :
8 : //find free path
9 83 : for(i = 0; i < pool->count; i++){
10 76 : if(!pool->paths[i]->in_transaction){
11 41 : pool->paths[i]->in_transaction = true;
12 41 : logprintf(log, LOG_INFO, "Reusing path pool slot %d\n", i);
13 41 : return pool->paths[i];
14 : }
15 : }
16 :
17 : //reallocate if needed
18 7 : if(pool->count < CMAIL_MAX_POOLED_PATHS){
19 7 : pool->paths = realloc(pool->paths, sizeof(MAILPATH*) * (pool->count + 1));
20 7 : if(!pool->paths){
21 0 : logprintf(log, LOG_ERROR, "Failed to reallocate path pool to %d entries\n", pool->count + 1);
22 0 : return NULL;
23 : }
24 :
25 7 : pool->paths[pool->count] = calloc(1, sizeof(MAILPATH));
26 7 : if(!pool->paths[pool->count]){
27 0 : logprintf(log, LOG_ERROR, "Failed to allocate path pool entry\n");
28 0 : return NULL;
29 : }
30 :
31 7 : path_reset(pool->paths[pool->count]);
32 7 : logprintf(log, LOG_DEBUG, "Reallocated path pool to %d entries\n", pool->count + 1);
33 7 : pool->paths[pool->count]->in_transaction = true;
34 :
35 7 : return pool->paths[pool->count++];
36 : }
37 : else{
38 0 : logprintf(log, LOG_WARNING, "Path pool exhausted\n");
39 0 : return NULL;
40 : }
41 : }
42 :
43 48 : void pathpool_return(MAILPATH* path){
44 48 : if(!path){
45 48 : return;
46 : }
47 :
48 48 : path_reset(path);
49 :
50 : //redundant, but oh well
51 48 : path->in_transaction = false;
52 : }
53 :
54 1 : void pathpool_free(PATHPOOL* pool){
55 : unsigned i;
56 :
57 1 : if(!pool){
58 1 : return;
59 : }
60 :
61 1 : if(pool->paths){
62 8 : for(i = 0; i < pool->count; i++){
63 7 : path_reset(pool->paths[i]);
64 7 : free(pool->paths[i]);
65 7 : pool->paths[i] = NULL;
66 : }
67 1 : free(pool->paths);
68 : }
69 :
70 1 : pool->paths = NULL;
71 1 : pool->count = 0;
72 : }
|