This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-maat/src/maat_garbage_collection.c

128 lines
2.9 KiB
C
Raw Normal View History

2022-11-17 05:05:35 +08:00
/*
**********************************************************************************************
* File: maat_garbage_collection.c
2022-11-17 05:05:35 +08:00
* Description:
* Authors: Zhengchao <zhengchao@geedgenetworks.com>
* Date: 2022-10-31
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
2022-11-17 05:05:35 +08:00
***********************************************************************************************
*/
#include <time.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <assert.h>
2023-02-03 17:28:14 +08:00
#include "maat_utils.h"
2022-11-17 05:05:35 +08:00
struct maat_garbage_bag {
time_t create_time;
int timeout;
int ok_times;
void *garbage;
void *arg;
void (* garbage_free)(void *garbage, void *arg);
2022-11-17 05:05:35 +08:00
TAILQ_ENTRY(maat_garbage_bag) entries;
};
TAILQ_HEAD(maat_garbage_q, maat_garbage_bag);
struct maat_garbage_bin {
struct maat_garbage_q garbage_q;
size_t bag_cnt;
int timeout_seconds;
};
2023-01-31 20:39:53 +08:00
struct maat_garbage_bin *maat_garbage_bin_new(int default_timeout)
2022-11-17 05:05:35 +08:00
{
struct maat_garbage_bin* bin = ALLOC(struct maat_garbage_bin, 1);
TAILQ_INIT(&bin->garbage_q);
bin->timeout_seconds = default_timeout;
return bin;
}
void maat_garbage_bin_free(struct maat_garbage_bin *bin)
2022-11-17 05:05:35 +08:00
{
if (NULL == bin) {
return;
}
2022-11-17 05:05:35 +08:00
struct maat_garbage_bag *p = NULL;
2023-02-09 22:13:15 +08:00
while ((p = TAILQ_FIRST(&bin->garbage_q)) != NULL) {
p->garbage_free(p->garbage, p->arg);
if (p->arg != NULL) {
FREE(p->arg);
}
2022-11-17 05:05:35 +08:00
TAILQ_REMOVE(&bin->garbage_q, p, entries);
2022-12-05 23:21:18 +08:00
FREE(p);
2022-11-17 05:05:35 +08:00
bin->bag_cnt--;
}
2022-12-05 23:21:18 +08:00
FREE(bin);
2022-11-17 05:05:35 +08:00
}
size_t maat_garbage_bin_get_size(struct maat_garbage_bin *bin)
2022-11-17 05:05:35 +08:00
{
if (NULL == bin) {
return 0;
}
2022-11-17 05:05:35 +08:00
return bin->bag_cnt;
}
void maat_garbage_bagging(struct maat_garbage_bin *bin, void *garbage, void *arg,
void (* func)(void *, void *))
2022-11-17 05:05:35 +08:00
{
struct maat_garbage_bag *bag = ALLOC(struct maat_garbage_bag, 1);
bag->create_time = time(NULL);
bag->timeout = bin->timeout_seconds;
bag->garbage = garbage;
2023-04-03 19:01:26 +08:00
bag->arg = arg;
2022-11-17 05:05:35 +08:00
bag->garbage_free = func;
TAILQ_INSERT_TAIL(&bin->garbage_q, bag, entries);
bin->bag_cnt++;
}
void maat_garbage_collect_routine(struct maat_garbage_bin *bin)
2022-11-17 05:05:35 +08:00
{
struct maat_garbage_bag *p = NULL, *tmp = NULL;
size_t n_clollected = 0, n_bag = 0;
time_t now = time(NULL);
for (p = TAILQ_FIRST(&bin->garbage_q); p != NULL; p = tmp) {
tmp = TAILQ_NEXT(p, entries);
if ((now - p->create_time) > p->timeout || p->timeout == 0) {
p->garbage_free(p->garbage, p->arg);
if (p->arg != NULL) {
FREE(p->arg);
}
2022-11-17 05:05:35 +08:00
TAILQ_REMOVE(&bin->garbage_q, p, entries);
2022-12-05 23:21:18 +08:00
FREE(p);
2022-11-17 05:05:35 +08:00
n_clollected++;
}
n_bag++;
}
assert(bin->bag_cnt == n_bag);
bin->bag_cnt -= n_clollected;
}
void maat_garbage_collect_by_force(struct maat_garbage_bin *bin)
2022-11-17 05:05:35 +08:00
{
struct maat_garbage_bag *p = NULL;
2023-02-09 22:13:15 +08:00
while ((p = TAILQ_FIRST(&bin->garbage_q)) != NULL) {
p->garbage_free(p->garbage, p->arg);
if (p->arg != NULL) {
FREE(p->arg);
}
2022-11-17 05:05:35 +08:00
TAILQ_REMOVE(&bin->garbage_q, p, entries);
2022-12-05 23:21:18 +08:00
FREE(p);
2022-11-17 05:05:35 +08:00
bin->bag_cnt--;
}
2023-04-03 19:01:26 +08:00
}