沉淀之log4c的stack

    log4c实现了栈操作的一组接口,相关的接口都在头文件里面进行了声明,这里栈的元素为指针,也就是说栈里面放的元素都是指向一个个结构体变量再内存中的位置的指针;另外这个栈是一个动态增长的形式实现的。
对于头文件的解释也都以注释的形式写在了代码的注释里面,具体的头文件内容如下:
stack.h
  1. #ifndef __sd_stack_h
  2. #define __sd_stack_h
  3. /**
  4. * @file stack.h @ingroup sd
  5. *
  6. * @brief Generic stack object.
  7. *
  8. * @todo documentation
  9. * @todo API homogeneity with sd_list and sd_hash
  10. */
  11. #include <stddef.h>
  12. #include "defs.h"
  13. __SD_BEGIN_DECLS
  14. //栈的结构体类型,具体的结构体定义在c文件中
  15. typedef struct __sd_stack sd_stack_t;
  16. //创建一个栈结构,参数是这个栈的容量
  17. extern sd_stack_t* sd_stack_new(size_t max);
  18. //删除一个栈结构,参数是栈的名字和释放接口
  19. extern void sd_stack_delete(sd_stack_t* astack, void (*free_data_fn)(void *));
  20. //获得栈中当前有多少元素,参数为要查看的栈
  21. extern size_t sd_stack_get_nelem(const sd_stack_t* astack);
  22. //清空栈,参数为待清空的栈和释放接口
  23. extern void sd_stack_clear(sd_stack_t* astack, void (*free_data_fn)(void *));
  24. //入栈操作,参数为栈和待入栈的数据指针
  25. extern int sd_stack_push(sd_stack_t* astack, void *data);
  26. //出栈操作,参数为栈,返回值为出栈的数据
  27. extern void* sd_stack_pop(sd_stack_t* astack);
  28. //获得参数传入的栈的第一个位置的元素
  29. extern void* sd_stack_begin(sd_stack_t* astack);
  30. //获取参数传入栈的下一个位置的元素内容
  31. extern void* sd_stack_next(sd_stack_t* astack);
  32. //获取参数传入栈的最后一个位置的元素内容
  33. extern void* sd_stack_end(sd_stack_t* astack);
  34. //获得参数传入栈的栈顶元素
  35. extern void* sd_stack_peek(sd_stack_t* astack);
  36. __SD_END_DECLS
  37. #endif
    里面同样有defs的一套东西,之前error里已经说过了。
    接下来就是stack的实现:
  1. #include <stdlib.h>
  2. #include <limits.h>
  3. #include <assert.h>
  4. #include "malloc.h"
  5. #include "stack.h"
  6. #define SD_STACK_INIT_SIZE 32
  7. struct __sd_stack {
  8. size_t max; //栈内元素最大个数,即栈容量
  9. size_t sp; //当前栈顶
  10. size_t size; //当前栈中设置的数量
  11. size_t iter; //栈内元素偏移指针
  12. void **array;
  13. };
    以上是头文件包含和栈的结构体定义。具体都在里面进行了注释
sd_stack_new:
  1. sd_stack_t* sd_stack_new(size_t max)
  2. {
  3. sd_stack_t* this;
  4. //申请一个栈,大小为栈的结构体
  5. this = sd_calloc(1, sizeof(sd_stack_t));
  6. //栈的容量、元素大小、当前栈顶位置
  7. this->max = max == 0 ? INT_MAX : max;
  8. this->size = SD_STACK_INIT_SIZE;
  9. this->sp = 0;
  10. //申请参数传递的数量个栈元素,申请内存类型为void *
  11. this->array = sd_calloc(this->size, sizeof(*this->array));
  12. return this;
  13. }
sd_stack_delete:
  1. void sd_stack_delete(sd_stack_t* this, void (*free_data_fn)(void *))
  2. {
  3. //参数有效性判断
  4. if (!this)
  5. return;
  6. //调用清除接口
  7. sd_stack_clear(this, free_data_fn);
  8. //释放内存
  9. free(this->array);
  10. free(this);
  11. }
调用的清除内存接口实现如下:
sd_stack_clear:
  1. void sd_stack_clear(sd_stack_t* this, void (*free_data_fn)(void *))
  2. {
  3. if (!this)
  4. return;
  5. //逐个释放栈元素所指向的内存
  6. if (free_data_fn) {
  7. while (this->sp > 0) {
  8. free_data_fn(this->array[--(this->sp)]);
  9. }
  10. }
  11. }
sd_stack_get_nelem:
  1. size_t sd_stack_get_nelem(const sd_stack_t* this)
  2. {
  3. return this ? this->sp : -1;
  4. }

sd_stack_begin:
  1. void* sd_stack_begin(sd_stack_t* this)
  2. {
  3. if (!this)
  4. return NULL;
  5. //元素数组偏移设置为0后进行返回
  6. this->iter = 0;
  7. return this->array[this->iter];
  8. }
sd_stack_next:
  1. void* sd_stack_next(sd_stack_t* this)
  2. {
  3. //判断栈有效,并且当前iter偏移不高于sp返回iter的元素值
  4. if (this && this->iter < this->sp)
  5. return this->array[this->iter++];
  6. return NULL;
  7. }
sd_stack_end:
  1. void* sd_stack_end(sd_stack_t* this)
  2. {
  3. return sd_stack_peek(this);
  4. }
里面借用的sd_stack_peek的接口,实现如下
sd_stack_peek:
  1. void* sd_stack_peek(sd_stack_t* this)
  2. {
  3. if (!this || !this->sp)
  4. return NULL;
  5. //返回栈顶元素
  6. return this->array[this->sp - 1];
  7. }
sd_stack_push:
  1. int sd_stack_push(sd_stack_t* this, void *data)
  2. {
  3. if (this == NULL)
  4. return -1;
  5. //当前入栈个数已经达到了设置的数量,扩充
  6. if (this->sp == this->size)
  7. {
  8. size_t new_size;
  9. //是否达到了最大数量
  10. if (this->size == this->max)
  11. return -1;
  12. //设置为最大数量或者为当前数量的二倍
  13. if (this->size * 2 > this->max) {
  14. new_size = this->max;
  15. } else {
  16. new_size = this->size * 2;
  17. }
  18. //重新进行元素栈内存的分配,realloc呵呵
  19. this->size = new_size;
  20. this->array = sd_realloc(this->array, sizeof(*this->array) * this->size);
  21. }
  22. //正确性验证
  23. assert(this->sp <= this->size);
  24. //入栈
  25. this->array[this->sp++] = data;
  26. return 0;
  27. }
sd_stack_pop:
  1. void* sd_stack_pop(sd_stack_t* this)
  2. {
  3. if (this == NULL || this->sp == 0)
  4. return NULL;
  5. //判断栈不为空且栈中数量可以进行缩减。其实不建议这么搞
  6. if (this->size >= SD_STACK_INIT_SIZE * 4 && this->sp < this->size / 4) {
  7. size_t new_size = this->size / 2;
  8. this->size = new_size;
  9. this->array = sd_realloc(this->array, sizeof(*this->array) * this->size);
  10. }
  11. //验证数据并进行出栈操作
  12. assert(this->sp > 0 && this->sp <= this->size);
  13. return this->array[--(this->sp)];
  14. }


比较简单明了的实现。




















原文地址:https://www.cnblogs.com/cfzhang/p/a32ac91d5b2d334dc3f410b6469d287f.html