Rev 968 | Rev 999 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 968 | Rev 985 | ||
|---|---|---|---|
| Line 581... | Line 581... | ||
| 581 | 581 | ||
| 582 | #define M_TRIM_THRESHOLD (-1) |
582 | #define M_TRIM_THRESHOLD (-1) |
| 583 | #define M_GRANULARITY (-2) |
583 | #define M_GRANULARITY (-2) |
| 584 | #define M_MMAP_THRESHOLD (-3) |
584 | #define M_MMAP_THRESHOLD (-3) |
| 585 | 585 | ||
| 586 | /* ------------------------ Mallinfo declarations ------------------------ */ |
586 | /** Non-default helenos customizations */ |
| - | 587 | #define LACKS_FCNTL_H |
|
| - | 588 | #define LACKS_SYS_MMAN_H |
|
| - | 589 | #define LACKS_SYS_PARAM_H |
|
| - | 590 | #undef HAVE_MMAP |
|
| - | 591 | #define HAVE_MMAP 0 |
|
| - | 592 | #define LACKS_ERRNO_H |
|
| - | 593 | /* Set errno? */ |
|
| - | 594 | #undef MALLOC_FAILURE_ACTION |
|
| - | 595 | #define MALLOC_FAILURE_ACTION |
|
| 587 | 596 | ||
| 588 | #if !NO_MALLINFO |
- | |
| 589 | /* |
- | |
| 590 | This version of malloc supports the standard SVID/XPG mallinfo |
- | |
| 591 | routine that returns a struct containing usage properties and |
- | |
| 592 | statistics. It should work on any system that has a |
- | |
| 593 | /usr/include/malloc.h defining struct mallinfo. The main |
- | |
| 594 | declaration needed is the mallinfo struct that is returned (by-copy) |
- | |
| 595 | by mallinfo(). The malloinfo struct contains a bunch of fields that |
- | |
| 596 | are not even meaningful in this version of malloc. These fields are |
- | |
| 597 | are instead filled by mallinfo() with other numbers that might be of |
- | |
| 598 | interest. |
- | |
| 599 | - | ||
| 600 | HAVE_USR_INCLUDE_MALLOC_H should be set if you have a |
- | |
| 601 | /usr/include/malloc.h file that includes a declaration of struct |
- | |
| 602 | mallinfo. If so, it is included; else a compliant version is |
- | |
| 603 | declared below. These must be precisely the same for mallinfo() to |
- | |
| 604 | work. The original SVID version of this struct, defined on most |
- | |
| 605 | systems with mallinfo, declares all fields as ints. But some others |
- | |
| 606 | define as unsigned long. If your system defines the fields using a |
- | |
| 607 | type of different width than listed here, you MUST #include your |
- | |
| 608 | system version and #define HAVE_USR_INCLUDE_MALLOC_H. |
- | |
| 609 | */ |
- | |
| 610 | - | ||
| 611 | /* #define HAVE_USR_INCLUDE_MALLOC_H */ |
- | |
| 612 | - | ||
| 613 | #ifdef HAVE_USR_INCLUDE_MALLOC_H |
- | |
| 614 | #include "/usr/include/malloc.h" |
- | |
| 615 | #else /* HAVE_USR_INCLUDE_MALLOC_H */ |
- | |
| 616 | - | ||
| 617 | struct mallinfo { |
- | |
| 618 | MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ |
- | |
| 619 | MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ |
- | |
| 620 | MALLINFO_FIELD_TYPE smblks; /* always 0 */ |
- | |
| 621 | MALLINFO_FIELD_TYPE hblks; /* always 0 */ |
- | |
| 622 | MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ |
- | |
| 623 | MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ |
- | |
| 624 | MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ |
- | |
| 625 | MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ |
- | |
| 626 | MALLINFO_FIELD_TYPE fordblks; /* total free space */ |
- | |
| 627 | MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ |
- | |
| 628 | }; |
- | |
| 629 | - | ||
| 630 | #endif /* HAVE_USR_INCLUDE_MALLOC_H */ |
- | |
| 631 | #endif /* NO_MALLINFO */ |
- | |
| 632 | - | ||
| 633 | #ifdef __cplusplus |
- | |
| 634 | extern "C" { |
- | |
| 635 | #endif /* __cplusplus */ |
- | |
| 636 | - | ||
| 637 | #if !ONLY_MSPACES |
- | |
| 638 | - | ||
| 639 | /* ------------------- Declarations of public routines ------------------- */ |
- | |
| 640 | - | ||
| 641 | #ifndef USE_DL_PREFIX |
- | |
| 642 | #define dlcalloc calloc |
- | |
| 643 | #define dlfree free |
- | |
| 644 | #define dlmalloc malloc |
- | |
| 645 | #define dlmemalign memalign |
- | |
| 646 | #define dlrealloc realloc |
- | |
| 647 | #define dlvalloc valloc |
- | |
| 648 | #define dlpvalloc pvalloc |
- | |
| 649 | #define dlmallinfo mallinfo |
- | |
| 650 | #define dlmallopt mallopt |
- | |
| 651 | #define dlmalloc_trim malloc_trim |
- | |
| 652 | #define dlmalloc_stats malloc_stats |
- | |
| 653 | #define dlmalloc_usable_size malloc_usable_size |
- | |
| 654 | #define dlmalloc_footprint malloc_footprint |
- | |
| 655 | #define dlmalloc_max_footprint malloc_max_footprint |
- | |
| 656 | #define dlindependent_calloc independent_calloc |
- | |
| 657 | #define dlindependent_comalloc independent_comalloc |
- | |
| 658 | #endif /* USE_DL_PREFIX */ |
- | |
| 659 | - | ||
| 660 | - | ||
| 661 | /* |
- | |
| 662 | malloc(size_t n) |
- | |
| 663 | Returns a pointer to a newly allocated chunk of at least n bytes, or |
- | |
| 664 | null if no space is available, in which case errno is set to ENOMEM |
- | |
| 665 | on ANSI C systems. |
- | |
| 666 | - | ||
| 667 | If n is zero, malloc returns a minimum-sized chunk. (The minimum |
- | |
| 668 | size is 16 bytes on most 32bit systems, and 32 bytes on 64bit |
- | |
| 669 | systems.) Note that size_t is an unsigned type, so calls with |
- | |
| 670 | arguments that would be negative if signed are interpreted as |
- | |
| 671 | requests for huge amounts of space, which will often fail. The |
- | |
| 672 | maximum supported value of n differs across systems, but is in all |
- | |
| 673 | cases less than the maximum representable value of a size_t. |
- | |
| 674 | */ |
- | |
| 675 | void* dlmalloc(size_t); |
- | |
| 676 | - | ||
| 677 | /* |
- | |
| 678 | free(void* p) |
- | |
| 679 | Releases the chunk of memory pointed to by p, that had been previously |
- | |
| 680 | allocated using malloc or a related routine such as realloc. |
- | |
| 681 | It has no effect if p is null. If p was not malloced or already |
- | |
| 682 | freed, free(p) will by default cause the current program to abort. |
- | |
| 683 | */ |
- | |
| 684 | void dlfree(void*); |
- | |
| 685 | - | ||
| 686 | /* |
- | |
| 687 | calloc(size_t n_elements, size_t element_size); |
- | |
| 688 | Returns a pointer to n_elements * element_size bytes, with all locations |
- | |
| 689 | set to zero. |
- | |
| 690 | */ |
- | |
| 691 | void* dlcalloc(size_t, size_t); |
- | |
| 692 | - | ||
| 693 | /* |
- | |
| 694 | realloc(void* p, size_t n) |
- | |
| 695 | Returns a pointer to a chunk of size n that contains the same data |
- | |
| 696 | as does chunk p up to the minimum of (n, p's size) bytes, or null |
- | |
| 697 | if no space is available. |
- | |
| 698 | - | ||
| 699 | The returned pointer may or may not be the same as p. The algorithm |
- | |
| 700 | prefers extending p in most cases when possible, otherwise it |
- | |
| 701 | employs the equivalent of a malloc-copy-free sequence. |
- | |
| 702 | - | ||
| 703 | If p is null, realloc is equivalent to malloc. |
- | |
| 704 | - | ||
| 705 | If space is not available, realloc returns null, errno is set (if on |
- | |
| 706 | ANSI) and p is NOT freed. |
- | |
| 707 | - | ||
| 708 | if n is for fewer bytes than already held by p, the newly unused |
- | |
| 709 | space is lopped off and freed if possible. realloc with a size |
- | |
| 710 | argument of zero (re)allocates a minimum-sized chunk. |
- | |
| 711 | - | ||
| 712 | The old unix realloc convention of allowing the last-free'd chunk |
- | |
| 713 | to be used as an argument to realloc is not supported. |
- | |
| 714 | */ |
- | |
| 715 | - | ||
| 716 | void* dlrealloc(void*, size_t); |
- | |
| 717 | - | ||
| 718 | /* |
- | |
| 719 | memalign(size_t alignment, size_t n); |
- | |
| 720 | Returns a pointer to a newly allocated chunk of n bytes, aligned |
- | |
| 721 | in accord with the alignment argument. |
- | |
| 722 | - | ||
| 723 | The alignment argument should be a power of two. If the argument is |
- | |
| 724 | not a power of two, the nearest greater power is used. |
- | |
| 725 | 8-byte alignment is guaranteed by normal malloc calls, so don't |
- | |
| 726 | bother calling memalign with an argument of 8 or less. |
- | |
| 727 | - | ||
| 728 | Overreliance on memalign is a sure way to fragment space. |
- | |
| 729 | */ |
- | |
| 730 | void* dlmemalign(size_t, size_t); |
- | |
| 731 | - | ||
| 732 | /* |
- | |
| 733 | valloc(size_t n); |
- | |
| 734 | Equivalent to memalign(pagesize, n), where pagesize is the page |
- | |
| 735 | size of the system. If the pagesize is unknown, 4096 is used. |
- | |
| 736 | */ |
- | |
| 737 | void* dlvalloc(size_t); |
- | |
| 738 | - | ||
| 739 | /* |
- | |
| 740 | mallopt(int parameter_number, int parameter_value) |
- | |
| 741 | Sets tunable parameters The format is to provide a |
- | |
| 742 | (parameter-number, parameter-value) pair. mallopt then sets the |
- | |
| 743 | corresponding parameter to the argument value if it can (i.e., so |
- | |
| 744 | long as the value is meaningful), and returns 1 if successful else |
- | |
| 745 | 0. SVID/XPG/ANSI defines four standard param numbers for mallopt, |
- | |
| 746 | normally defined in malloc.h. None of these are use in this malloc, |
- | |
| 747 | so setting them has no effect. But this malloc also supports other |
- | |
| 748 | options in mallopt. See below for details. Briefly, supported |
- | |
| 749 | parameters are as follows (listed defaults are for "typical" |
- | |
| 750 | configurations). |
- | |
| 751 | - | ||
| 752 | Symbol param # default allowed param values |
- | |
| 753 | M_TRIM_THRESHOLD -1 2*1024*1024 any (MAX_SIZE_T disables) |
- | |
| 754 | M_GRANULARITY -2 page size any power of 2 >= page size |
- | |
| 755 | M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) |
- | |
| 756 | */ |
- | |
| 757 | int dlmallopt(int, int); |
- | |
| 758 | - | ||
| 759 | /* |
- | |
| 760 | malloc_footprint(); |
- | |
| 761 | Returns the number of bytes obtained from the system. The total |
- | |
| 762 | number of bytes allocated by malloc, realloc etc., is less than this |
- | |
| 763 | value. Unlike mallinfo, this function returns only a precomputed |
- | |
| 764 | result, so can be called frequently to monitor memory consumption. |
- | |
| 765 | Even if locks are otherwise defined, this function does not use them, |
- | |
| 766 | so results might not be up to date. |
- | |
| 767 | */ |
- | |
| 768 | size_t dlmalloc_footprint(void); |
- | |
| 769 | - | ||
| 770 | /* |
- | |
| 771 | malloc_max_footprint(); |
- | |
| 772 | Returns the maximum number of bytes obtained from the system. This |
- | |
| 773 | value will be greater than current footprint if deallocated space |
- | |
| 774 | has been reclaimed by the system. The peak number of bytes allocated |
- | |
| 775 | by malloc, realloc etc., is less than this value. Unlike mallinfo, |
- | |
| 776 | this function returns only a precomputed result, so can be called |
- | |
| 777 | frequently to monitor memory consumption. Even if locks are |
- | |
| 778 | otherwise defined, this function does not use them, so results might |
- | |
| 779 | not be up to date. |
- | |
| 780 | */ |
- | |
| 781 | size_t dlmalloc_max_footprint(void); |
- | |
| 782 | - | ||
| 783 | #if !NO_MALLINFO |
- | |
| 784 | /* |
- | |
| 785 | mallinfo() |
- | |
| 786 | Returns (by copy) a struct containing various summary statistics: |
- | |
| 787 | - | ||
| 788 | arena: current total non-mmapped bytes allocated from system |
- | |
| 789 | ordblks: the number of free chunks |
- | |
| 790 | smblks: always zero. |
- | |
| 791 | hblks: current number of mmapped regions |
- | |
| 792 | hblkhd: total bytes held in mmapped regions |
- | |
| 793 | usmblks: the maximum total allocated space. This will be greater |
- | |
| 794 | than current total if trimming has occurred. |
- | |
| 795 | fsmblks: always zero |
- | |
| 796 | uordblks: current total allocated space (normal or mmapped) |
- | |
| 797 | fordblks: total free space |
- | |
| 798 | keepcost: the maximum number of bytes that could ideally be released |
- | |
| 799 | back to system via malloc_trim. ("ideally" means that |
- | |
| 800 | it ignores page restrictions etc.) |
- | |
| 801 | - | ||
| 802 | Because these fields are ints, but internal bookkeeping may |
- | |
| 803 | be kept as longs, the reported values may wrap around zero and |
- | |
| 804 | thus be inaccurate. |
- | |
| 805 | */ |
- | |
| 806 | struct mallinfo dlmallinfo(void); |
- | |
| 807 | #endif /* NO_MALLINFO */ |
- | |
| 808 | - | ||
| 809 | /* |
- | |
| 810 | independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); |
- | |
| 811 | - | ||
| 812 | independent_calloc is similar to calloc, but instead of returning a |
- | |
| 813 | single cleared space, it returns an array of pointers to n_elements |
- | |
| 814 | independent elements that can hold contents of size elem_size, each |
- | |
| 815 | of which starts out cleared, and can be independently freed, |
- | |
| 816 | realloc'ed etc. The elements are guaranteed to be adjacently |
- | |
| 817 | allocated (this is not guaranteed to occur with multiple callocs or |
- | |
| 818 | mallocs), which may also improve cache locality in some |
- | |
| 819 | applications. |
- | |
| 820 | - | ||
| 821 | The "chunks" argument is optional (i.e., may be null, which is |
- | |
| 822 | probably the most typical usage). If it is null, the returned array |
- | |
| 823 | is itself dynamically allocated and should also be freed when it is |
- | |
| 824 | no longer needed. Otherwise, the chunks array must be of at least |
- | |
| 825 | n_elements in length. It is filled in with the pointers to the |
- | |
| 826 | chunks. |
- | |
| 827 | - | ||
| 828 | In either case, independent_calloc returns this pointer array, or |
- | |
| 829 | null if the allocation failed. If n_elements is zero and "chunks" |
- | |
| 830 | is null, it returns a chunk representing an array with zero elements |
- | |
| 831 | (which should be freed if not wanted). |
- | |
| 832 | - | ||
| 833 | Each element must be individually freed when it is no longer |
- | |
| 834 | needed. If you'd like to instead be able to free all at once, you |
- | |
| 835 | should instead use regular calloc and assign pointers into this |
- | |
| 836 | space to represent elements. (In this case though, you cannot |
- | |
| 837 | independently free elements.) |
- | |
| 838 | - | ||
| 839 | independent_calloc simplifies and speeds up implementations of many |
- | |
| 840 | kinds of pools. It may also be useful when constructing large data |
- | |
| 841 | structures that initially have a fixed number of fixed-sized nodes, |
- | |
| 842 | but the number is not known at compile time, and some of the nodes |
- | |
| 843 | may later need to be freed. For example: |
- | |
| 844 | - | ||
| 845 | struct Node { int item; struct Node* next; }; |
- | |
| 846 | - | ||
| 847 | struct Node* build_list() { |
- | |
| 848 | struct Node** pool; |
- | |
| 849 | int n = read_number_of_nodes_needed(); |
- | |
| 850 | if (n <= 0) return 0; |
- | |
| 851 | pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); |
- | |
| 852 | if (pool == 0) die(); |
- | |
| 853 | // organize into a linked list... |
- | |
| 854 | struct Node* first = pool[0]; |
- | |
| 855 | for (i = 0; i < n-1; ++i) |
- | |
| 856 | pool[i]->next = pool[i+1]; |
- | |
| 857 | free(pool); // Can now free the array (or not, if it is needed later) |
- | |
| 858 | return first; |
- | |
| 859 | } |
- | |
| 860 | */ |
- | |
| 861 | void** dlindependent_calloc(size_t, size_t, void**); |
- | |
| 862 | - | ||
| 863 | /* |
- | |
| 864 | independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); |
- | |
| 865 | - | ||
| 866 | independent_comalloc allocates, all at once, a set of n_elements |
- | |
| 867 | chunks with sizes indicated in the "sizes" array. It returns |
- | |
| 868 | an array of pointers to these elements, each of which can be |
- | |
| 869 | independently freed, realloc'ed etc. The elements are guaranteed to |
- | |
| 870 | be adjacently allocated (this is not guaranteed to occur with |
- | |
| 871 | multiple callocs or mallocs), which may also improve cache locality |
- | |
| 872 | in some applications. |
- | |
| 873 | - | ||
| 874 | The "chunks" argument is optional (i.e., may be null). If it is null |
- | |
| 875 | the returned array is itself dynamically allocated and should also |
- | |
| 876 | be freed when it is no longer needed. Otherwise, the chunks array |
- | |
| 877 | must be of at least n_elements in length. It is filled in with the |
- | |
| 878 | pointers to the chunks. |
- | |
| 879 | - | ||
| 880 | In either case, independent_comalloc returns this pointer array, or |
- | |
| 881 | null if the allocation failed. If n_elements is zero and chunks is |
- | |
| 882 | null, it returns a chunk representing an array with zero elements |
- | |
| 883 | (which should be freed if not wanted). |
- | |
| 884 | - | ||
| 885 | Each element must be individually freed when it is no longer |
- | |
| 886 | needed. If you'd like to instead be able to free all at once, you |
- | |
| 887 | should instead use a single regular malloc, and assign pointers at |
- | |
| 888 | particular offsets in the aggregate space. (In this case though, you |
- | |
| 889 | cannot independently free elements.) |
- | |
| 890 | - | ||
| 891 | independent_comallac differs from independent_calloc in that each |
- | |
| 892 | element may have a different size, and also that it does not |
- | |
| 893 | automatically clear elements. |
- | |
| 894 | - | ||
| 895 | independent_comalloc can be used to speed up allocation in cases |
- | |
| 896 | where several structs or objects must always be allocated at the |
- | |
| 897 | same time. For example: |
- | |
| 898 | - | ||
| 899 | struct Head { ... } |
- | |
| 900 | struct Foot { ... } |
- | |
| 901 | - | ||
| 902 | void send_message(char* msg) { |
- | |
| 903 | int msglen = strlen(msg); |
- | |
| 904 | size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; |
- | |
| 905 | void* chunks[3]; |
- | |
| 906 | if (independent_comalloc(3, sizes, chunks) == 0) |
- | |
| 907 | die(); |
- | |
| 908 | struct Head* head = (struct Head*)(chunks[0]); |
- | |
| 909 | char* body = (char*)(chunks[1]); |
- | |
| 910 | struct Foot* foot = (struct Foot*)(chunks[2]); |
- | |
| 911 | // ... |
- | |
| 912 | } |
- | |
| 913 | - | ||
| 914 | In general though, independent_comalloc is worth using only for |
- | |
| 915 | larger values of n_elements. For small values, you probably won't |
- | |
| 916 | detect enough difference from series of malloc calls to bother. |
- | |
| 917 | - | ||
| 918 | Overuse of independent_comalloc can increase overall memory usage, |
- | |
| 919 | since it cannot reuse existing noncontiguous small chunks that |
- | |
| 920 | might be available for some of the elements. |
- | |
| 921 | */ |
- | |
| 922 | void** dlindependent_comalloc(size_t, size_t*, void**); |
- | |
| 923 | - | ||
| 924 | - | ||
| 925 | /* |
- | |
| 926 | pvalloc(size_t n); |
- | |
| 927 | Equivalent to valloc(minimum-page-that-holds(n)), that is, |
- | |
| 928 | round up n to nearest pagesize. |
- | |
| 929 | */ |
- | |
| 930 | void* dlpvalloc(size_t); |
- | |
| 931 | - | ||
| 932 | /* |
- | |
| 933 | malloc_trim(size_t pad); |
- | |
| 934 | - | ||
| 935 | If possible, gives memory back to the system (via negative arguments |
- | |
| 936 | to sbrk) if there is unused memory at the `high' end of the malloc |
- | |
| 937 | pool or in unused MMAP segments. You can call this after freeing |
- | |
| 938 | large blocks of memory to potentially reduce the system-level memory |
- | |
| 939 | requirements of a program. However, it cannot guarantee to reduce |
- | |
| 940 | memory. Under some allocation patterns, some large free blocks of |
- | |
| 941 | memory will be locked between two used chunks, so they cannot be |
- | |
| 942 | given back to the system. |
- | |
| 943 | - | ||
| 944 | The `pad' argument to malloc_trim represents the amount of free |
- | |
| 945 | trailing space to leave untrimmed. If this argument is zero, only |
- | |
| 946 | the minimum amount of memory to maintain internal data structures |
- | |
| 947 | will be left. Non-zero arguments can be supplied to maintain enough |
- | |
| 948 | trailing space to service future expected allocations without having |
- | |
| 949 | to re-obtain memory from the system. |
- | |
| 950 | - | ||
| 951 | Malloc_trim returns 1 if it actually released any memory, else 0. |
- | |
| 952 | */ |
- | |
| 953 | int dlmalloc_trim(size_t); |
- | |
| 954 | - | ||
| 955 | /* |
- | |
| 956 | malloc_usable_size(void* p); |
- | |
| 957 | - | ||
| 958 | Returns the number of bytes you can actually use in |
- | |
| 959 | an allocated chunk, which may be more than you requested (although |
- | |
| 960 | often not) due to alignment and minimum size constraints. |
- | |
| 961 | You can use this many bytes without worrying about |
- | |
| 962 | overwriting other allocated objects. This is not a particularly great |
- | |
| 963 | programming practice. malloc_usable_size can be more useful in |
- | |
| 964 | debugging and assertions, for example: |
- | |
| 965 | - | ||
| 966 | p = malloc(n); |
- | |
| 967 | assert(malloc_usable_size(p) >= 256); |
- | |
| 968 | */ |
- | |
| 969 | size_t dlmalloc_usable_size(void*); |
- | |
| 970 | - | ||
| 971 | /* |
- | |
| 972 | malloc_stats(); |
- | |
| 973 | Prints on stderr the amount of space obtained from the system (both |
- | |
| 974 | via sbrk and mmap), the maximum amount (which may be more than |
- | |
| 975 | current if malloc_trim and/or munmap got called), and the current |
- | |
| 976 | number of bytes allocated via malloc (or realloc, etc) but not yet |
- | |
| 977 | freed. Note that this is the number of bytes allocated, not the |
- | |
| 978 | number requested. It will be larger than the number requested |
- | |
| 979 | because of alignment and bookkeeping overhead. Because it includes |
- | |
| 980 | alignment wastage as being in use, this figure may be greater than |
- | |
| 981 | zero even when no user-level chunks are allocated. |
- | |
| 982 | - | ||
| 983 | The reported current and maximum system memory can be inaccurate if |
- | |
| 984 | a program makes other calls to system memory allocation functions |
- | |
| 985 | (normally sbrk) outside of malloc. |
- | |
| 986 | - | ||
| 987 | malloc_stats prints only the most commonly interesting statistics. |
- | |
| 988 | More information can be obtained by calling mallinfo. |
- | |
| 989 | */ |
- | |
| 990 | void dlmalloc_stats(void); |
- | |
| 991 | - | ||
| 992 | #endif /* ONLY_MSPACES */ |
- | |
| 993 | - | ||
| 994 | #if MSPACES |
- | |
| 995 | - | ||
| 996 | /* |
- | |
| 997 | mspace is an opaque type representing an independent |
- | |
| 998 | region of space that supports mspace_malloc, etc. |
- | |
| 999 | */ |
- | |
| 1000 | typedef void* mspace; |
- | |
| 1001 | - | ||
| 1002 | /* |
- | |
| 1003 | create_mspace creates and returns a new independent space with the |
- | |
| 1004 | given initial capacity, or, if 0, the default granularity size. It |
- | |
| 1005 | returns null if there is no system memory available to create the |
- | |
| 1006 | space. If argument locked is non-zero, the space uses a separate |
- | |
| 1007 | lock to control access. The capacity of the space will grow |
- | |
| 1008 | dynamically as needed to service mspace_malloc requests. You can |
- | |
| 1009 | control the sizes of incremental increases of this space by |
- | |
| 1010 | compiling with a different DEFAULT_GRANULARITY or dynamically |
- | |
| 1011 | setting with mallopt(M_GRANULARITY, value). |
- | |
| 1012 | */ |
- | |
| 1013 | mspace create_mspace(size_t capacity, int locked); |
- | |
| 1014 | - | ||
| 1015 | /* |
- | |
| 1016 | destroy_mspace destroys the given space, and attempts to return all |
- | |
| 1017 | of its memory back to the system, returning the total number of |
- | |
| 1018 | bytes freed. After destruction, the results of access to all memory |
- | |
| 1019 | used by the space become undefined. |
- | |
| 1020 | */ |
- | |
| 1021 | size_t destroy_mspace(mspace msp); |
- | |
| 1022 | - | ||
| 1023 | /* |
- | |
| 1024 | create_mspace_with_base uses the memory supplied as the initial base |
- | |
| 1025 | of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this |
- | |
| 1026 | space is used for bookkeeping, so the capacity must be at least this |
- | |
| 1027 | large. (Otherwise 0 is returned.) When this initial space is |
- | |
| 1028 | exhausted, additional memory will be obtained from the system. |
- | |
| 1029 | Destroying this space will deallocate all additionally allocated |
- | |
| 1030 | space (if possible) but not the initial base. |
- | |
| 1031 | */ |
- | |
| 1032 | mspace create_mspace_with_base(void* base, size_t capacity, int locked); |
- | |
| 1033 | - | ||
| 1034 | /* |
- | |
| 1035 | mspace_malloc behaves as malloc, but operates within |
- | |
| 1036 | the given space. |
- | |
| 1037 | */ |
- | |
| 1038 | void* mspace_malloc(mspace msp, size_t bytes); |
- | |
| 1039 | - | ||
| 1040 | /* |
- | |
| 1041 | mspace_free behaves as free, but operates within |
- | |
| 1042 | the given space. |
- | |
| 1043 | - | ||
| 1044 | If compiled with FOOTERS==1, mspace_free is not actually needed. |
- | |
| 1045 | free may be called instead of mspace_free because freed chunks from |
- | |
| 1046 | any space are handled by their originating spaces. |
- | |
| 1047 | */ |
- | |
| 1048 | void mspace_free(mspace msp, void* mem); |
- | |
| 1049 | - | ||
| 1050 | /* |
- | |
| 1051 | mspace_realloc behaves as realloc, but operates within |
- | |
| 1052 | the given space. |
- | |
| 1053 | - | ||
| 1054 | If compiled with FOOTERS==1, mspace_realloc is not actually |
- | |
| 1055 | needed. realloc may be called instead of mspace_realloc because |
- | |
| 1056 | realloced chunks from any space are handled by their originating |
- | |
| 1057 | spaces. |
- | |
| 1058 | */ |
- | |
| 1059 | void* mspace_realloc(mspace msp, void* mem, size_t newsize); |
- | |
| 1060 | - | ||
| 1061 | /* |
- | |
| 1062 | mspace_calloc behaves as calloc, but operates within |
- | |
| 1063 | the given space. |
- | |
| 1064 | */ |
- | |
| 1065 | void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); |
- | |
| 1066 | - | ||
| 1067 | /* |
- | |
| 1068 | mspace_memalign behaves as memalign, but operates within |
- | |
| 1069 | the given space. |
- | |
| 1070 | */ |
- | |
| 1071 | void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); |
- | |
| 1072 | - | ||
| 1073 | /* |
- | |
| 1074 | mspace_independent_calloc behaves as independent_calloc, but |
- | |
| 1075 | operates within the given space. |
- | |
| 1076 | */ |
- | |
| 1077 | void** mspace_independent_calloc(mspace msp, size_t n_elements, |
- | |
| 1078 | size_t elem_size, void* chunks[]); |
- | |
| 1079 | - | ||
| 1080 | /* |
- | |
| 1081 | mspace_independent_comalloc behaves as independent_comalloc, but |
- | |
| 1082 | operates within the given space. |
- | |
| 1083 | */ |
- | |
| 1084 | void** mspace_independent_comalloc(mspace msp, size_t n_elements, |
- | |
| 1085 | size_t sizes[], void* chunks[]); |
- | |
| 1086 | - | ||
| 1087 | /* |
- | |
| 1088 | mspace_footprint() returns the number of bytes obtained from the |
- | |
| 1089 | system for this space. |
- | |
| 1090 | */ |
- | |
| 1091 | size_t mspace_footprint(mspace msp); |
- | |
| 1092 | - | ||
| 1093 | /* |
- | |
| 1094 | mspace_max_footprint() returns the peak number of bytes obtained from the |
- | |
| 1095 | system for this space. |
- | |
| 1096 | */ |
- | |
| 1097 | size_t mspace_max_footprint(mspace msp); |
- | |
| 1098 | - | ||
| 1099 | - | ||
| 1100 | #if !NO_MALLINFO |
- | |
| 1101 | /* |
- | |
| 1102 | mspace_mallinfo behaves as mallinfo, but reports properties of |
- | |
| 1103 | the given space. |
- | |
| 1104 | */ |
- | |
| 1105 | struct mallinfo mspace_mallinfo(mspace msp); |
- | |
| 1106 | #endif /* NO_MALLINFO */ |
- | |
| 1107 | - | ||
| 1108 | /* |
- | |
| 1109 | mspace_malloc_stats behaves as malloc_stats, but reports |
- | |
| 1110 | properties of the given space. |
- | |
| 1111 | */ |
- | |
| 1112 | void mspace_malloc_stats(mspace msp); |
- | |
| 1113 | - | ||
| 1114 | /* |
- | |
| 1115 | mspace_trim behaves as malloc_trim, but |
- | |
| 1116 | operates within the given space. |
- | |
| 1117 | */ |
- | |
| 1118 | int mspace_trim(mspace msp, size_t pad); |
- | |
| 1119 | - | ||
| 1120 | /* |
- | |
| 1121 | An alias for mallopt. |
- | |
| 1122 | */ |
- | |
| 1123 | int mspace_mallopt(int, int); |
- | |
| 1124 | - | ||
| 1125 | #endif /* MSPACES */ |
- | |
| 1126 | - | ||
| 1127 | #ifdef __cplusplus |
- | |
| 1128 | }; /* end of extern "C" */ |
- | |
| 1129 | #endif /* __cplusplus */ |
- | |
| 1130 | 597 | ||
| 1131 | /* |
598 | /* |
| 1132 | ======================================================================== |
599 | ======================================================================== |
| 1133 | To make a fully customizable malloc.h header file, cut everything |
600 | To make a fully customizable malloc.h header file, cut everything |
| 1134 | above this line, put into file malloc.h, edit to suit, and #include it |
601 | above this line, put into file malloc.h, edit to suit, and #include it |
| 1135 | on the next line, as well as in programs that use this malloc. |
602 | on the next line, as well as in programs that use this malloc. |
| 1136 | ======================================================================== |
603 | ======================================================================== |
| 1137 | */ |
604 | */ |
| 1138 | 605 | ||
| 1139 | /* #include "malloc.h" */ |
606 | #include "malloc.h" |
| 1140 | 607 | ||
| 1141 | /*------------------------------ internal #includes ---------------------- */ |
608 | /*------------------------------ internal #includes ---------------------- */ |
| 1142 | 609 | ||
| 1143 | #ifdef WIN32 |
610 | #ifdef WIN32 |
| 1144 | #pragma warning( disable : 4146 ) /* no "unsigned" warnings */ |
611 | #pragma warning( disable : 4146 ) /* no "unsigned" warnings */ |