Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發中 Java 與 C++ 通用的系統常量(路徑、檔名、後綴最大長度等)

編輯:關於 Android 程式設計

在 Android 的 Java 層與 Native(C++)層交互時,經常會用到一些由 Linux 內核頭檔案自動生成的系統常量,例如路徑最大長度、檔名最大長度、可開啟檔案數上限等。這些定義被 bionic libc 引入,供使用者態程式呼叫內核時參考。下面是一個典型的 linux/limits.h 頭檔案內容:

/**************************************************************************** **************************************************************************** *** *** This header was automatically generated from a Linux kernel header *** of the same name, to make information necessary for userspace to *** call into the kernel available to libc. It contains only constants, *** structures, and macros generated from the original header, and thus, *** contains no copyrightable information. *** *** To edit the content of this header, modify the corresponding *** source file (e.g. under external/kernel-headers/original/) then *** run bionic/libc/kernel/tools/update_all.py *** *** Any manual change here will be lost the next time this script will *** be run. You've been warned! *** *** **************************************************************************** ****************************************************************************/ #ifndef _LINUX_LIMITS_H #define _LINUX_LIMITS_H #define NR_OPEN 1024 #define NGROUPS_MAX 65536 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ #define ARG_MAX 131072 #define CHILD_MAX 999 #define OPEN_MAX 256 #define LINK_MAX 127 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ #define MAX_CANON 255 #define MAX_INPUT 255 #define NAME_MAX 255 #define PATH_MAX 4096 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ #define PIPE_BUF 4096 #define XATTR_NAME_MAX 255 #define XATTR_SIZE_MAX 65536 #define XATTR_LIST_MAX 65536 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ #define RTSIG_MAX 32 #endif

其中幾個較常用的常量含義如下:

NAME_MAX(255):單個檔名(不含路徑)的最大位元組數。

PATH_MAX(4096):完整路徑字串的最大長度。

OPEN_MAX(256):單行程式可同時開啟的檔案描述符數量上限。

PIPE_BUF(4096):管道一次原子寫入的最大位元組數。

需要特別注意的是,這份檔案開頭反覆強調「不要手動修改」,因為它是由腳本從內核源碼自動生成的,任何手改都會在下次執行 update_all.py 時被覆蓋。若真要調整,應去修改 external/kernel-headers/original/ 下對應的原始檔案,再重新執行腳本生成。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved