OpenJDK源码赏析之四(jli_util中的工具函数)
生活随笔
收集整理的這篇文章主要介紹了
OpenJDK源码赏析之四(jli_util中的工具函数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇:
?OpenJDK源碼賞析之三:Java命令參數的讀取_星空_MAX的博客-CSDN博客
不承接上一篇,這篇單獨開始分析jli_util.h(java工具函數)里的一些函數
JLI_MemAlloc
/** Returns a pointer to a block of at least 'size' bytes of memory.* Prints error message and exits if the memory could not be allocated.*/ void * JLI_MemAlloc(size_t size) {void *p = malloc(size);if (p == 0) {perror("malloc");exit(1);}return p; }可以看到jJLT里面的內存分配實際上用了C語言自帶的malloc函數,這里如果分配失敗則會輸出錯誤信息,并執行,exit(1)表示異常退出,這個1是返回給操作系統的
關于malloc函數精解,可以看另一篇文章
linux-malloc底層實現原理_mmshixing的博客-CSDN博客_malloc的底層實現
JLT_MemeRealloc
/** Equivalent to realloc(size).* Prints error message and exits if the memory could not be reallocated.*/ void * JLI_MemRealloc(void *ptr, size_t size) {void *p = realloc(ptr, size);if (p == 0) {perror("realloc");exit(1);}return p; }重新分配內存,也同樣是調用C語言的
JLI_StringDup
/** Wrapper over strdup(3C) which prints an error message and exits if memory* could not be allocated.*/ char * JLI_StringDup(const char *s1) {char *s = strdup(s1);if (s == NULL) {perror("strdup");exit(1);}return s; }strdup可以直接把要復制的內容賦值給沒有初始化的指針,因為它會自動分配空間給目的指針
strcpy的目的指針一定是已經分配內存的指針
JLI_MemFree
/** Very equivalent to free(ptr).* Here to maintain pairing with the above routines.*/ void JLI_MemFree(void *ptr) {free(ptr); }呃,和free一樣,官方解釋為和上面保持一樣
JLI_StrCCmp
int JLI_StrCCmp(const char *s1, const char* s2) {return JLI_StrNCmp(s1, s2, JLI_StrLen(s2)); }在jli_util.h中找到宏定義
#define JLI_StrNCmp(p1, p2, p3) strncmp((p1), (p2), (p3))將p1函數和p2函數進行比較,比較p3個字節,
p1=p2則等于0,
p1>p2則返回值大于0,
p1<p2則返回值小于0
總結
以上是生活随笔為你收集整理的OpenJDK源码赏析之四(jli_util中的工具函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL消费表中查找所有用户最后一条消费记
- 下一篇: 牛客扫盲行动