Virtual memory Linux
Ներածություն Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses by the memory management hardware. This mapping is defined by page tables, set up by the operating system. Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses by the memory management hardware. This mapping is defined by page tables, set up by the operating system.
Վիրտուալ – ֆիզիկական արտապատկերում #include #include phys_addr = virt_to_phys(virt_addr); virt_addr = phys_to_virt(phys_addr); bus_addr = virt_to_bus(virt_addr); virt_addr = bus_to_virt(bus_addr);
Էջային կազմակերպումը The basic unit of memory is the page. Nobody knows how large a page is, this is architecture-dependent, but typically PAGE_SIZE = 4096 The basic unit of memory is the page. Nobody knows how large a page is, this is architecture-dependent, but typically PAGE_SIZE = 4096
get_free_page The routine __get_free_page() will give us a page. The routine __get_free_page() will give us a page. The routine __get_free_pages() will give a number of consecutive pages. The routine __get_free_pages() will give a number of consecutive pages.
vmalloc The routine vmalloc() has a similar purpose, but has a better chance of being able to return larger consecutive areas, and is more expensive. It uses page table manipulation to create an area of memory that is consecutive in virtual memory, but not necessarily in physical memory. The routine vmalloc() has a similar purpose, but has a better chance of being able to return larger consecutive areas, and is more expensive. It uses page table manipulation to create an area of memory that is consecutive in virtual memory, but not necessarily in physical memory.
Օրինակ 1 #include #include int main() { int n = 0; int n = 0; while (1) while (1) { if (malloc(1<<20) == NULL) if (malloc(1<<20) == NULL) { printf("malloc failure after %d MiB\n", n); return 0; } printf ("got %d MiB\n", ++n); printf ("got %d MiB\n", ++n);}}
Օրինակ 2 #include #include int main () { int n = 0; char *p; int n = 0; char *p; while (1) { while (1) { if ((p = malloc(1<<20)) == NULL) { if ((p = malloc(1<<20)) == NULL) { printf("malloc failure after %d MiB\n", n); printf("malloc failure after %d MiB\n", n); return 0; return 0; } memset (p, 0, (1<<20)); memset (p, 0, (1<<20)); printf ("got %d MiB\n", ++n); printf ("got %d MiB\n", ++n); } }
Օրինակ 3 #include #include #define N int main () { int i, n = 0; int i, n = 0; char *pp[N]; char *pp[N]; for (n = 0; n < N; n++) { for (n = 0; n < N; n++) { pp[n] = malloc(1<<20); if (pp[n] == NULL) pp[n] = malloc(1<<20); if (pp[n] == NULL) break; break; } printf("malloc failure after %d MiB\n", n); printf("malloc failure after %d MiB\n", n); for (i = 0; i < n; i++) { for (i = 0; i < n; i++) { memset (pp[i], 0, (1<<20)); memset (pp[i], 0, (1<<20)); printf("%d\n", i+1); printf("%d\n", i+1); } return 0; return 0;}