michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsISupports.idl" michael@0: michael@0: /** michael@0: * michael@0: * nsIMemory: interface to allocate and deallocate memory. Also provides michael@0: * for notifications in low-memory situations. michael@0: * michael@0: * The frozen exported symbols NS_Alloc, NS_Realloc, and NS_Free michael@0: * provide a more efficient way to access XPCOM memory allocation. Using michael@0: * those symbols is preferred to using the methods on this interface. michael@0: * michael@0: * A client that wishes to be notified of low memory situations (for michael@0: * example, because the client maintains a large memory cache that michael@0: * could be released when memory is tight) should register with the michael@0: * observer service (see nsIObserverService) using the topic michael@0: * "memory-pressure". There are three specific types of notications michael@0: * that can occur. These types will be passed as the |aData| michael@0: * parameter of the of the "memory-pressure" notification: michael@0: * michael@0: * "low-memory" michael@0: * This will be passed as the extra data when the pressure michael@0: * observer is being asked to flush for low-memory conditions. michael@0: * michael@0: * "low-memory-ongoing" michael@0: * This will be passed when we continue to be in a low-memory michael@0: * condition and we want to flush caches and do other cheap michael@0: * forms of memory minimization, but heavy handed approaches like michael@0: * a GC are unlikely to succeed. michael@0: * michael@0: * "-no-forward" michael@0: * This is appended to the above two parameters when the resulting michael@0: * notification should not be forwarded to the child processes. michael@0: * michael@0: * "heap-minimize" michael@0: * This will be passed as the extra data when the pressure michael@0: * observer is being asked to flush because of a heap minimize michael@0: * call. michael@0: * michael@0: * "alloc-failure" michael@0: * This will be passed as the extra data when the pressure michael@0: * observer has been asked to flush because a malloc() or michael@0: * realloc() has failed. michael@0: */ michael@0: michael@0: [scriptable, uuid(6aef11c4-8615-44a6-9711-98f43805693d)] michael@0: interface nsIMemory : nsISupports michael@0: { michael@0: /** michael@0: * Allocates a block of memory of a particular size. If the memory michael@0: * cannot be allocated (because of an out-of-memory condition), the michael@0: * process aborts. michael@0: * michael@0: * @param size - the size of the block to allocate michael@0: * @result the block of memory michael@0: */ michael@0: [noscript, notxpcom] voidPtr alloc(in size_t size); michael@0: michael@0: /** michael@0: * Reallocates a block of memory to a new size. michael@0: * michael@0: * @param ptr - the block of memory to reallocate michael@0: * @param size - the new size michael@0: * @result the reallocated block of memory michael@0: * michael@0: * If ptr is null, this function behaves like malloc. michael@0: * If s is the size of the block to which ptr points, the first michael@0: * min(s, size) bytes of ptr's block are copied to the new block. michael@0: * If the allocation succeeds, ptr is freed and a pointer to the michael@0: * new block returned. If the allocation fails, the process aborts. michael@0: */ michael@0: [noscript, notxpcom] voidPtr realloc(in voidPtr ptr, michael@0: in size_t newSize); michael@0: michael@0: /** michael@0: * Frees a block of memory. Null is a permissible value, in which case michael@0: * nothing happens. michael@0: * michael@0: * @param ptr - the block of memory to free michael@0: */ michael@0: [noscript, notxpcom] void free(in voidPtr ptr); michael@0: michael@0: /** michael@0: * Attempts to shrink the heap. michael@0: * @param immediate - if true, heap minimization will occur michael@0: * immediately if the call was made on the main thread. If michael@0: * false, the flush will be scheduled to happen when the app is michael@0: * idle. michael@0: * @throws NS_ERROR_FAILURE if 'immediate' is set an the call michael@0: * was not on the application's main thread. michael@0: */ michael@0: void heapMinimize(in boolean immediate); michael@0: michael@0: /** michael@0: * This predicate can be used to determine if we're in a low-memory michael@0: * situation (what constitutes low-memory is platform dependent). This michael@0: * can be used to trigger the memory pressure observers. michael@0: * michael@0: * DEPRECATED - Always returns false. See bug 592308. michael@0: */ michael@0: boolean isLowMemory(); michael@0: michael@0: /** michael@0: * This predicate can be used to determine if the platform is a "low-memory" michael@0: * platform. Callers may use this to dynamically tune their behaviour michael@0: * to favour reduced memory usage at the expense of performance. The value michael@0: * returned by this function will not change over the lifetime of the process. michael@0: */ michael@0: boolean isLowMemoryPlatform(); michael@0: }; michael@0: