了解

structc-https://github.com/wangzhione/structc

  1. structc C 构建基础项目框架. 不是太惊艳, 但绝对是 C 简单项目中一股清流.
  2. 它的前身是 simplec 框架.

simplec - https://github.com/wangzhione/simplec

  1. 二者相比. structc 框架更加自然. 力求贴合 C 项目开发的原始状态. 所有写的代码, 心愿就是
  2. 向着标准库, 操作系统, 编译器靠拢!
  3. 例如下面代码
  1. #ifndef _H_THREAD
  2. #define _H_THREAD
  3. #include <struct.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. //
  7. // pthread_run - 异步启动线程
  8. // id : &tid 线程id地址
  9. // frun : 运行的主体
  10. // arg : 运行参数
  11. // return : 返回线程构建结果, 0 is success
  12. //
  13. #define pthread_run(id, frun, arg) \
  14. pthread_run_((id), (node_f)(frun), (void *)(intptr_t)(arg))
  15. inline int pthread_run_(pthread_t * id, node_f frun, void * arg) {
  16. return pthread_create(id, NULL, (start_f)frun, arg);
  17. }
  18. //
  19. // pthread_end - 等待启动线程结束
  20. // tid : 线程id
  21. // return : void
  22. //
  23. inline void pthread_end(pthread_t tid) {
  24. pthread_join(tid, NULL);
  25. }
  26. //
  27. // pthread_async - 异步启动分离线程
  28. // frun : 运行的主体
  29. // arg : 运行参数
  30. // return : 返回 0 is success
  31. //
  32. #define pthread_async(frun, arg) \
  33. pthread_async_((node_f)(frun), (void *)(intptr_t)(arg))
  34. inline int pthread_async_(node_f frun, void * arg) {
  35. int ret;
  36. pthread_t tid;
  37. pthread_attr_t attr;
  38. pthread_attr_init(&attr);
  39. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  40. ret = pthread_create(&tid, &attr, (start_f)frun, arg);
  41. pthread_attr_destroy(&attr);
  42. return ret;
  43. }
  44. #endif//_H_THREAD
  1. 有时候想问为什么喜欢用 C 写这些毫无营养的 东东. 在回答这个问题之前.
  2. 引述 golang.org 中一段代码
  1. // You can edit this code!
  2. // Click here and start typing.
  3. package main
  4. import "fmt"
  5. func main() {
  6. fmt.Println("Hello, 世界")
  7. }
  1. (: real 的, 在写的溜语言中, 唯独 Go 很好用, * 真丑.)
  2. 猜测可能还是, C 有点意思 ~

借 - https://y.qq.com/n/yqq/song/002WLDmw0vkHtC.html

  1. 曾经多少个夜晚 在敲那些字符 printf("Hello World");

解说

  1. 不妨说说 structc 的构成
  2. 引进部分:
  3. 1. 内存池选用 jemalloc
  4. 2. 复用 IO 选用 libuv
  5. 3. 线程模型选用 pthtread
  6. 补充部分 : winds 争取实现 linux 一样的内容
  7. 1. errno, strerror 机制
  8. 2. socket 机制
  9. 3. select / epoll 机制
  10. 4. time 机制
  11. 5. atom 机制
  12. ... ...
  13. 核心部分
  14. 1. mq.h 队列 - https://github.com/wangzhione/structc/blob/master/structc/struct/mq.h
  15. 2. dict.h 字典 - https://github.com/wangzhione/structc/blob/master/structc/struct/dict.h
  16. 3. tstr.h 字符串 - https://github.com/wangzhione/structc/blob/master/structc/struct/tstr.h
  17. 4. list.h 单链表 - https://github.com/wangzhione/structc/blob/master/structc/struct/list.h
  18. 5. rtree.h 红黑树 - https://github.com/wangzhione/structc/blob/master/structc/struct/rtree.h
  19. 6. array.h 动态数组 - https://github.com/wangzhione/structc/blob/master/structc/struct/array.h
  20. ... ... 也许可以说, 数据结构 当前 仍是软件设计的源头吧.
  21. 本文虽然作为 structc 拉粉的软文. 但是感觉其中有些数据结构的设计思路很值得借鉴.
  22. 例如 mq.h 中队列 empty or full 思路
  1. //
  2. // pop empty <=> tail == -1 ( head == 0 )
  3. // push full <=> head == tail + 1
  4. //
  5. struct mq {
  6. int head; // 头结点
  7. int tail; // 尾结点
  8. int size; // 队列大小
  9. void ** queue; // 队列实体
  10. atom_t lock; // 队列原子锁
  11. volatile bool fee; // true表示销毁状态
  12. };
  1. dict.h 中关于素数表的引入
  1. //
  2. // primes - 质数表
  3. //
  4. const unsigned primes[][2] = {
  5. { (1<<6)-1 , 53 },
  6. { (1<<7)-1 , 97 },
  7. { (1<<8)-1 , 193 },
  8. { (1<<9)-1 , 389 },
  9. { (1<<10)-1 , 769 },
  10. { (1<<11)-1 , 1543 },
  11. { (1<<12)-1 , 3079 },
  12. { (1<<13)-1 , 6151 },
  13. { (1<<14)-1 , 12289 },
  14. { (1<<15)-1 , 24593 },
  15. { (1<<16)-1 , 49157 },
  16. { (1<<17)-1 , 98317 },
  17. { (1<<18)-1 , 196613 },
  18. { (1<<19)-1 , 393241 },
  19. { (1<<20)-1 , 786433 },
  20. { (1<<21)-1 , 1572869 },
  21. { (1<<22)-1 , 3145739 },
  22. { (1<<23)-1 , 6291469 },
  23. { (1<<24)-1 , 12582917 },
  24. { (1<<25)-1 , 25165843 },
  25. { (1<<26)-1 , 50331653 },
  26. { (1<<27)-1 , 100663319 },
  27. { (1<<28)-1 , 201326611 },
  28. { (1<<29)-1 , 402653189 },
  29. { (1<<30)-1 , 805306457 },
  30. { UINT_MAX , 1610612741 },
  31. };
  1. 说起 (1<<6) - 1 不妨问问大家 (2 ^ 6) - 1 是多少 ? 是不是也很有意思 : )
  2. 或者 rtree.h
  1. //
  2. // 红黑树通用结构, 需要将 $RTREE 放在结构开头部位
  3. //
  4. struct $rtree {
  5. uintptr_t parentc;
  6. struct $rtree * left;
  7. struct $rtree * right;
  8. };
  1. 等等 ... ... 不一一列举. structc 的代码很有实战参照意义.
  2. 有兴趣的同学可以详细看看, 顺带肉眼帮我提提 BUG, 在此表示感谢 . : )

后继

  1. 错误是难免的, 欢迎指正.
  2. 这里最后开启程序员写代码模式, 挑个函数结尾
  1. // _str_printf : 成功直接返回
  2. static char * _str_printf(const char * format, va_list arg) {
  3. char buf[BUFSIZ];
  4. int len = vsnprintf(buf, sizeof buf, format, arg);
  5. if (len < sizeof buf) {
  6. char * ret = malloc(len + 1);
  7. return memcpy(ret, buf, len + 1);
  8. }
  9. return NULL;
  10. }
  11. //
  12. // str_printf - 字符串构建函数
  13. // format : 构建格式参照pritnf
  14. // ... : 参数集
  15. // return : char * 堆上内存
  16. //
  17. char *
  18. str_printf(const char * format, ...) {
  19. char * ret;
  20. int len, cap;
  21. va_list arg;
  22. va_start(arg, format);
  23. // BUFSIZ 以下内存直接分配
  24. ret = _str_printf(format, arg);
  25. if (ret != NULL)
  26. return ret;
  27. cap = BUFSIZ << 1;
  28. for (;;) {
  29. ret = malloc(cap);
  30. len = vsnprintf(ret, cap, format, arg);
  31. // 失败的情况
  32. if (len < 0) {
  33. free(ret);
  34. return NULL;
  35. }
  36. // 成功情况
  37. if (len < cap)
  38. break;
  39. // 内存不足的情况
  40. free(ret);
  41. cap <<= 1;
  42. }
  43. return realloc(ret, len + 1);
  44. }
  1. 人生 一块开心 最好 : )

structc 开源框架简介的更多相关文章

  1. [转]六款值得推荐的android(安卓)开源框架简介

    本文转自:http://www.jb51.net/article/51052.htm .volley 项目地址 https://github.com/smanikandan14/Volley-demo ...

  2. 6个值得推荐的Android开源框架简介(转)

    虽然我们在做app的时候并不一定用到框架,但是一些好框架的思想是非常有学习价值的 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo  ...

  3. 六款值得推荐的android(安卓)开源框架简介(转)

    1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JSON,图像等的异步下载: (2)  网络请求的排序(scheduli ...

  4. 几款值得推荐的android(安卓)开源框架简介

    技术不再多,知道一些常用的.不错的就够了. 该文章自有需要的时候,mark一下. 顺序不代表排名,根据自己需求进行选择即可. 1.volley 项目地址 https://github.com/sman ...

  5. 六款值得推荐的android(安卓)开源框架简介

    1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JSON,图像等的异步下载: (2)  网络请求的排序(scheduli ...

  6. 六款值得推荐的android(安卓)开源框架简介【转】

    http://my.oschina.net/u/1244156/blog/380647 1.volley 项目地址 https://github.com/smanikandan14/Volley-de ...

  7. 六款值得推荐的Android开源框架简介

    技术不再多,知道一些常用的.不错的就够了.下面就是最近整理的“性价比”比较高的Android开源框架,应该是相对实用的. 1.volley 项目地址 https://github.com/smanik ...

  8. structc 开源框架介绍

    引言 - 一切才刚刚开始 structc 是 C 结构基础库. 简单可复用. structc - https://github.com/wangzhione/structc 之前也描述过几次 stru ...

  9. 开源框架是如何通过JMX来做监控的(一) - JMX简介和Standard MBean

    相关文章目录: 开源框架是如何通过JMX来做监控的(一) - JMX简介和Standard MBean 开源框架是如何通过JMX来做监控的(二) - Druid连接池的监控 相信很多做Java开发的同 ...

随机推荐

  1. [翻译] HTKDynamicResizingCell

    HTKDynamicResizingCell https://github.com/henrytkirk/HTKDynamicResizingCell Subclassed UITableView/U ...

  2. Linux iptables命令详解

    iptables命令主要是设置防火墙信息的 常见命令参数 Usage: iptables -[AD] chain rule-specification [options] iptables -I ch ...

  3. Analysis of Algorithms

    算法分析 Introduction 有各种原因要求我们分析算法,像预测算法性能,比较不同算法优劣等,其中很实际的一条原因是为了避免性能错误,要对自己算法的性能有个概念. 科学方法(scientific ...

  4. Autorelease 性能测试

    __weak NSString *string_weak_ = nil; - (void)viewDidLoad { [super viewDidLoad]; // 场景 1 NSString *st ...

  5. 图论——最短路径 Dijkstra算法、Floyd算法

    1.弗洛伊德算法(Floyd) 弗洛伊算法核心就是三重循环,M [ j ] [ k ] 表示从 j 到 k 的路径,而 i 表示当前 j 到 k 可以借助的点:红色部分表示,如果 j 到 i ,i 到 ...

  6. MySQL 分库分表方案,总结的非常好!

    前言 公司最近在搞服务分离,数据切分方面的东西,因为单张包裹表的数据量实在是太大,并且还在以每天60W的量增长. 之前了解过数据库的分库分表,读过几篇博文,但就只知道个模糊概念, 而且现在回想起来什么 ...

  7. 架构图以及vue的简介

    架构图 前后端分离总架构图 前端架构设计图 MVVM架构模式 MVVM的简介 MVVM 由 Model,View,ViewModel 三部分构成,Model 层代表数据模型,也可以在Model中定义数 ...

  8. Selenium & Webdriver 远程测试和多线程并发测试

    Selenium & Webdriver 远程测试和多线程并发测试 Selenium Webdriver自动化测试,初学者可以使用selenium ide录制脚本,然后生成java程序导入ec ...

  9. ZCMU 2177 Lucky Numbers (easy)

    传送门: http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=2177 2177: Lucky Numbers (easy) 时间限制: 2 Sec   ...

  10. 用 S5PV210 学习 Linux (二) 刷机(二)

    1.在 Ubuntu 下 ,进入  dnw-linux-master\src\driver 文件下,make 截图 如下: 2.紧接着 加载该模块到内核(注意:需要root权限),sudo insmo ...