//內存出錯函數**封裝的一個報錯函數staticvoidsdsOomAbort(void){//發生內存錯誤時向標準輸出設備輸出出錯信息fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");abort();//出錯時直接終止程序}
/* Split 's' with separator in 'sep'. An array* of sds strings is returned. *count will be set* by reference to the number of tokens returned.** On out of memory, zero length string, zero length* separator, NULL is returned.** Note that 'sep' is able to split a string using* a multi-character separator. For example* sdssplit("foo_-_bar","_-_"); will return two* elements "foo" and "bar".** This version of the function is binary-safe but* requires length arguments. sdssplit() is just the* same function but for zero-terminated strings.*/
sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {int elements = 0, slots = 5, start = 0, j;//開設5字節的內存空間//實際開設時會開設6字節,具體可以參看zmallo.c里的源代碼sds *tokens = zmalloc(sizeof(sds)*slots);
#ifdef SDS_ABORT_ON_OOMif (tokens == NULL) sdsOomAbort();
#endifif (seplen < 1 || len < 0 || tokens == NULL) return NULL;for (j = 0; j < (len-(seplen-1)); j++) {/* make sure there is room for the next element and the final one */if (slots < elements+2) {sds *newtokens;slots *= 2;newtokens = zrealloc(tokens,sizeof(sds)*slots);if (newtokens == NULL) {
#ifdef SDS_ABORT_ON_OOMsdsOomAbort();
#elsegoto cleanup;
#endif}tokens = newtokens;}/* search the separator */if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {//計算分割符以前的字符串的長度tokens[elements] = sdsnewlen(s+start,j-start);if (tokens[elements] == NULL) {
#ifdef SDS_ABORT_ON_OOMsdsOomAbort();//失敗的情況下,如果定義了內存失敗函數,將直接調用
#elsegoto cleanup;//否則跳到cleanup函數,進行一個一個的釋放
#endif}elements++;start = j+seplen;j = j+seplen-1; /* skip the separator */}}/* Add the final element. We are sure there is room in the tokens array. *///分割串里面的最后一個字符串tokens[elements] = sdsnewlen(s+start,len-start);if (tokens[elements] == NULL) {
#ifdef SDS_ABORT_ON_OOMsdsOomAbort();
#elsegoto cleanup;
#endif}elements++;//傳的是個引用,可以獲取最終分割為多少個數組*count = elements;return tokens;#ifndef SDS_ABORT_ON_OOM
//如果沒有定義分配失敗的情況下,內存操作函數
//將會一個一個的調用內存失敗處理函數
cleanup:{int i;for (i = 0; i < elements; i++) sdsfree(tokens[i]);zfree(tokens);return NULL;}
#endif
}
為什么打開文件有^M
計算機還沒有出現之前,有一種叫做電傳打字機(Teletype Model 33)的玩意,每秒鐘可以打10個字符。但是它有一個問題,就是打完一行換行的時候,要用去0.2秒,正好可以打兩個字符…
文章目錄操作方式查看rdb文件參考文檔redis作者解釋rdb和aof的不同redisRDB文件格式Sripathi Krishnamredis各個版本變化操作方式
127.0.0.1:9999> flushall
OK
127.0.0.1:9999> set name hodge
OK
127.0.0.1:9999> save
OK查看rdb文件
[rootpython src]# od -c dum…