A complete set of memory control functions for Linux C

Source: Internet
Author: User
Tags gopher


Calloc (configure memory space)
Related functions Malloc, free, realloc, BRK
Header file # Include <stdlib. h>
Define functions Void * calloc (size_t nmemb, size_t size );
Function Description Calloc () is used to configure nmemb adjacent memory units. The size of each unit is size, and a pointer to the first element is returned. This works the same way as using malloc (nmemb * size); however, when using calloc () to configure memory, the memory content is initialized to 0.
Return Value If the configuration is successful, a pointer is returned. If the configuration fails, null is returned.
Example /* Dynamically configure 10 struct test Spaces */
# Include <stdlib. h>
Struct Test
{
Int A [10];
Char B [20];
}
Main ()
{
Struct test * PTR = calloc (sizeof (struct test), 10 );
}
 



Free (release the previously configured memory)
Related functions Malloc, calloc, realloc, BRK
Header file # Include <stdlib. h>
Define functions Void free (void * PTR );
Function Description The PTR parameter is a pointer to the memory previously returned by malloc (), calloc (), or realloc. After free () is called, the memory space indicated by PTR is reclaimed. If the memory space referred to by the PTR parameter has been withdrawn or the unknown memory address, calling free () may be unpredictable. If the PTR parameter is null, free () does not function.
 



Getpagesize (obtain the memory paging size)
Related functions Sbrk
Header file # Include <unistd. h>
Define functions Size_t getpagesize (void );
Function Description Returns the size of a page in bytes ). This is the system page size and may not necessarily be the same as the hardware page size.
Return Value Memory paging size. Additional instructions on Intel x86, the returned value is 4096 bytes.
Example # Include <unistd. h>
Main ()
{
Printf ("page size = % d/N", getpagesize ());
}
 



Malloc (configure memory space)
Related functions Calloc, free, realloc, BRK
Header file # Include <stdlib. h>
Define functions Void * malloc (size_t size );
Function Description Malloc () is used to configure the memory space, which is determined by the specified size.
Return Value If the configuration is successful, a pointer is returned. If the configuration fails, null is returned.
Example Void P = malloc (1024);/* configure 1 kb of memory */
 



MMAP (Create memory ing)
Related functions Munmap, open
Header file # Include <unistd. h>
# Include <sys/Mman. h>
Define functions Void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offsize );
Function Description MMAP () is used to map the content of a file to the memory. Access to the memory area is to directly read and write the file content. The start parameter points to the memory start address, which is usually set to null, indicating that the system will automatically select the address. After the address is successfully matched, it will return. The length parameter indicates that the large part of the file is mapped to the memory.
Parameters Prot indicates that the protection methods of the ing region are combined as follows:
The prot_exec ing area can be executed.
The prot_read ing area can be read.
The prot_write ing area can be written.
The prot_none ing area cannot be accessed.
Parameters Flags will affect the features of the ing area.
Map_fixed if the address specified by the start parameter cannot be mapped successfully, the ing is abandoned and the address is not corrected. This flag is generally not encouraged.
Map_shared copies data written to the ing area back to the file and allows other processes mapped to the file to share the data.
Map_private write operations on the ing area will generate a copy of The ing file, that is, private copy on write) no modifications made to this region will be written back to the original file content.
Map_anonymous creates an anonymous ing. The FD parameter is ignored, and files are not involved, and the ing area cannot be shared with other processes.
Map_denywrite only allows write operations on the ing area. Other operations that directly write data to the file will be rejected.
Map_locked locks the ing region, which means the region will not be replaced (SWAp ).
You must specify map_shared or map_private when calling MMAP. The FD parameter is the description of the file returned by open (), indicating the file to be mapped to the memory. The offset parameter is the offset of the file ing. It is usually set to 0, which indicates the correspondence starting from the beginning of the file. The offset must be an integer multiple of the page size.
Return Value If the ing is successful, the start address of memory in the ing area is returned. Otherwise, map_failed (-1) is returned. The error cause is stored in errno.
Error Code The ebadf parameter FD is not a valid file description.
The eacces access permission is incorrect. If map_private is used, the file must be readable. If map_shared is used, prot_write is required and the file must be writable.
Either start, length, or offset of the einval parameter is invalid.
The eagain file is locked or too much memory is locked.
Insufficient enomem memory.
Example /* Use MMAP () to read/etc/passwd file content */
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <sys/Mman. h>
Main ()
{
Int FD;
Void * start;
Struct stat Sb;
FD = open ("/etc/passwd", o_rdonly);/* Open/etc/passwd */
Fstat (FD, & SB);/* Get file size */
Start = MMAP (null, SB. st_size, prot_read, map_private, FD, 0 );
If (START = map_failed)/* determine whether the ing is successful */
Return;
Printf ("% s", start );
Munma (START, SB. st_size);/* unmap */
Closed (FD );
}
Run Root: X: 0: Root:/root:/bin/bash
Bin: X: 1: 1: Bin:/bin:
Daemon: X: 2: 2: daemon:/sbin
ADM: X: 3: 4: ADM:/var/adm:
LP: X: 4: 7: LP:/var/spool/lpd:
Sync: X: 5: 0: Sync:/sbin: Bin/Sync:
Shutdown: X: 6: 0: shutdown:/sbin/Shutdown
Halt: X: 7: 0: Halt:/sbin/halt
Mail: X: 8: 12: Mail:/var/spool/mail:
News: X: 9: 13: News:/var/spool/news:
Uucp: X: 10: 14: uucp:/var/spool/uucp:
Operator: X: 11: 0: Operator:/root:
Games: X: 12: 100: games:/usr/games:
Gopher: X: 13: 30: gopher:/usr/lib/gopher-data:
FTP: X: 14: 50: FTP user:/home/ftp:
Nobody: X: 99: 99: Nobody :/:
XFS: 100: 101: X Font Server:/etc/Xll/Fs:/bin/false
TPD: X: 42: 42:/home/TPD:/bin/bash
KIDS: X: 500: 500:/home/kids:/bin/bash
 



Munmap (remove memory ing)
Related functions MMAP
Header file # Include <unistd. h>
# Include <sys/Mman. h>
Define functions Int munmap (void * Start, size_t length );
Function Description Munmap () is used to cancel the ing memory start address referred to by the parameter start. The parameter length is the memory size to be canceled. When the process ends or other programs are executed using exec functions, the ing memory is automatically removed, but the ing is not removed when the corresponding file description is disabled.
Return Value If the ing is successfully unbound, 0 is returned; otherwise,-1 is returned. The error cause is stored in the error code einval in errno.
Parameters The START or length is invalid.
Example Refer to MMAP ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.