当前位置: 首页 > wzjs >正文

兴业大街网站建设wordpress 更新服务 搜狗

兴业大街网站建设,wordpress 更新服务 搜狗,网络营销软文范例500字,机械网站案例底层数据结构的好处: 杜绝缓冲区溢出。减少修改字符串长度时所需的内存重分配次数。二进制安全。兼容部分C字符串函数。 常用命令: set key value、get key 等 应用场景:共享 session、分布式锁,计数器、限流。 1、给char*定义…

底层数据结构的好处:

  1. 杜绝缓冲区溢出。
  2. 减少修改字符串长度时所需的内存重分配次数。
  3. 二进制安全。
  4. 兼容部分C字符串函数。

常用命令: set key value、get key 等

应用场景:共享 session、分布式锁,计数器、限流。

1、给char*定义了个别名。

typedef char *sds;

2、创建sds字符串并且分配空间

sds.c
sds结构体
/** 保存字符串对象的结构*/
struct sdshdr {// buf 中已占用空间的长度int len;// buf 中剩余可用空间的长度int free;// 数据空间char buf[];
};
//好处之一:创建sds字符串的时候会优先分配空间并且预留下一次分配空间。
sds sdsnewlen(const void *init, size_t initlen) {定义sds结构体指针struct sdshdr *sh;if (init) {//创建sds结构体并且分配空间sh = zmalloc(sizeof(struct sdshdr)+initlen+1);} else {sh = zcalloc(sizeof(struct sdshdr)+initlen+1);}if (sh == NULL) return NULL;sh->len = initlen;sh->free = 0;if (initlen && init)memcpy(sh->buf, init, initlen);sh->buf[initlen] = '\0';return (char*)sh->buf;
}

3、sds字符串的追加

/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the* end of the specified sds string 's'.** After the call, the passed sds string is no longer valid and all the* references must be substituted with the new pointer returned by the call. */
//s目标字符串
//t源字符串
//len追加的长度
sds sdscatlen(sds s, const void *t, size_t len) {struct sdshdr *sh;//计算目标字符串的长度size_t curlen = sdslen(s);//根据要追加的长度len和目标字符串s的现有长度,判断是否要增加新的空间s = sdsMakeRoomFor(s,len);if (s == NULL) return NULL;sh = (void*) (s-(sizeof(struct sdshdr)));//将源字符串t中len长度的数据拷贝到目标字符串结尾memcpy(s+curlen, t, len);sh->len = curlen+len;sh->free = sh->free-len;//追加\0作为本次追加的结尾。s[curlen+len] = '\0';return s;
}

        3-1、扩容详细。

,这是一个相对耗时的操作,这里尽量在使用的时候做好计算。

/* Enlarge the free space at the end of the sds string so that the caller* is sure that after calling this function can overwrite up to addlen* bytes after the end of the string, plus one more byte for nul term.* * Note: this does not change the *length* of the sds string as returned* by sdslen(), but only the free buffer space we have. * 当计算后的新的长度小于1MB,则分配两倍空间* 当计算后的新的长度大于1MB,则在原来基础上加多1MB。
*/
#define SDS_MAX_PREALLOC (1024*1024)
sds sdsMakeRoomFor(sds s, size_t addlen) {struct sdshdr *sh, *newsh;// 获取 s 目前的空余空间长度size_t free = sdsavail(s);size_t len, newlen;// s 目前的空余空间已经足够,无须再进行扩展,直接返回if (free >= addlen) return s;// 获取 s 目前已占用空间的长度len = sdslen(s);sh = (void*) (s-(sizeof(struct sdshdr)));// s 最少需要的长度newlen = (len+addlen);// 根据新长度,为 s 分配新空间所需的大小if (newlen < SDS_MAX_PREALLOC)// 如果新长度小于 SDS_MAX_PREALLOC // 那么为它分配两倍于所需长度的空间newlen *= 2;else// 否则,分配长度为目前长度加上 SDS_MAX_PREALLOCnewlen += SDS_MAX_PREALLOC;// T = O(N)newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);// 内存不足,分配失败,返回if (newsh == NULL) return NULL;// 更新 sds 的空余长度newsh->free = newlen - len;// 返回 sdsreturn newsh->buf;
}

4、SDS 类型

sdshdr5、sdshdr8、sdshdr16、sdshdr32、sdshdr64

 5种结构体类型,设计是一样的,字符数组现有长度 len 和分配空间长度 alloc是不一样的。

以sdshdr8结构体为例,

 __attribute__ ((__packed__))的作用:
//告诉编译器,在编译 sdshdr8 结构时,不要使用字节对齐的方式,而是采用紧凑的方式分配内存。这是因为在默认情况下,编译器会按照 8 字节对齐的方式,给变量分配内存。也就是说,即使一个变量的大小不到 8 个字节,编译器也会给它分配 8 个字节。

struct __attribute__ ((__packed__)) sdshdr8 {uint8_t len; /* 字符数组现有长度*/uint8_t alloc; /* 字符数组的已分配空间,不包括结构体和\0结束字符*/unsigned char flags; /* SDS类型*/char buf[]; /*字符数组*/
};

文章转载自:

http://DYaEJaRA.mLntx.cn
http://CLSF78Sx.mLntx.cn
http://NNfhZusV.mLntx.cn
http://l59AXKpE.mLntx.cn
http://uWtmZ6fS.mLntx.cn
http://tKn5jx0C.mLntx.cn
http://PN4dwl9p.mLntx.cn
http://q5bqZOk6.mLntx.cn
http://boSXtcCH.mLntx.cn
http://7AEEEySB.mLntx.cn
http://Yj8KxFrO.mLntx.cn
http://clBepN2m.mLntx.cn
http://xzdraDo2.mLntx.cn
http://R1n68pAK.mLntx.cn
http://uUm2O7pZ.mLntx.cn
http://8yenSmLI.mLntx.cn
http://GN98OarU.mLntx.cn
http://DShgaGpI.mLntx.cn
http://myrzIURh.mLntx.cn
http://LbB6kA7A.mLntx.cn
http://EKNoeRas.mLntx.cn
http://yZsfjNPl.mLntx.cn
http://x08dHAJ4.mLntx.cn
http://WB48D0B1.mLntx.cn
http://1DcxnBqy.mLntx.cn
http://UW4dmKR6.mLntx.cn
http://EcsiOiRU.mLntx.cn
http://nmbnPu0q.mLntx.cn
http://4pLz6a5w.mLntx.cn
http://gS1k4QlU.mLntx.cn
http://www.dtcms.com/wzjs/626316.html

相关文章:

  • 电子商务网站特点wordpress 修改登录页面
  • 网站页面不更新网页设计基础知识选择题
  • 宁金诚信建设网站河北沧为信息技术有限公司
  • 换接入商网站备案黄山找人做网站
  • 奇月网络官方网站无锡做网站专业的公司
  • 物流运输做网站的素材wordpress标签云添加图片
  • jquery制作简单的网页西安网站建设优化
  • 长沙网站建设公司做网站界面需要注意什么
  • 做网站建网站什么网站都有漏洞
  • 聊城网站建设设计开发公司永州网站建设服务
  • 潍坊模板建站定制网站怎么做留言板
  • 个人网站的首页seo推广技术
  • 湖南网站建设公司企业网站建设联系
  • 苏州网站开发公司济南兴田德润厉害吗重庆网站推广机构
  • 陕西网站建设培训电商店铺首页设计
  • 怎么做网络直播卖衣服的网站制作网页的详细步骤
  • 网站生成app要多少钱关于公司网络优化方案
  • 国家电网公司交流建设分公司网站苏州园区一站式服务中心
  • 肇庆网站开发公司inititle 网站建设
  • 外企网站建设手机免费代理ip网站
  • 网站营销公司东莞动点网络科技有限公司
  • 外国网站设计素材wordpress搬站换空间
  • 濮阳做网站的兰州网站建设推荐q479185700上墙
  • 常州高端网站定制公司站长是什么职位
  • 万网网站流量厦门网站建设哪里好
  • 网站建设外链实时积分榜
  • 北京各大网站推广平台哪家好沛县网站建设xlec
  • 做视频网站服务器怎么选择抵押网站建设方案
  • 一般建设网站的常见问题装修设计用什么软件好用
  • 网站租用空间中国建筑八个局排名