memory/mozalloc/mozalloc_oom.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: sw=4 ts=4 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #if defined(XP_WIN)
michael@0 9 # define MOZALLOC_EXPORT __declspec(dllexport)
michael@0 10 #endif
michael@0 11
michael@0 12 #include "mozilla/mozalloc_abort.h"
michael@0 13 #include "mozilla/mozalloc_oom.h"
michael@0 14 #include "mozilla/Assertions.h"
michael@0 15
michael@0 16 static mozalloc_oom_abort_handler gAbortHandler;
michael@0 17
michael@0 18 #define OOM_MSG_LEADER "out of memory: 0x"
michael@0 19 #define OOM_MSG_DIGITS "0000000000000000" // large enough for 2^64
michael@0 20 #define OOM_MSG_TRAILER " bytes requested"
michael@0 21 #define OOM_MSG_FIRST_DIGIT_OFFSET sizeof(OOM_MSG_LEADER) - 1
michael@0 22 #define OOM_MSG_LAST_DIGIT_OFFSET sizeof(OOM_MSG_LEADER) + \
michael@0 23 sizeof(OOM_MSG_DIGITS) - 3
michael@0 24
michael@0 25 static const char *hex = "0123456789ABCDEF";
michael@0 26
michael@0 27 void
michael@0 28 mozalloc_handle_oom(size_t size)
michael@0 29 {
michael@0 30 char oomMsg[] = OOM_MSG_LEADER OOM_MSG_DIGITS OOM_MSG_TRAILER;
michael@0 31 size_t i;
michael@0 32
michael@0 33 // NB: this is handle_oom() stage 1, which simply aborts on OOM.
michael@0 34 // we might proceed to a stage 2 in which an attempt is made to
michael@0 35 // reclaim memory
michael@0 36
michael@0 37 if (gAbortHandler)
michael@0 38 gAbortHandler(size);
michael@0 39
michael@0 40 static_assert(OOM_MSG_FIRST_DIGIT_OFFSET > 0,
michael@0 41 "Loop below will never terminate (i can't go below 0)");
michael@0 42
michael@0 43 // Insert size into the diagnostic message using only primitive operations
michael@0 44 for (i = OOM_MSG_LAST_DIGIT_OFFSET;
michael@0 45 size && i >= OOM_MSG_FIRST_DIGIT_OFFSET; i--) {
michael@0 46 oomMsg[i] = hex[size % 16];
michael@0 47 size /= 16;
michael@0 48 }
michael@0 49
michael@0 50 mozalloc_abort(oomMsg);
michael@0 51 }
michael@0 52
michael@0 53 void
michael@0 54 mozalloc_set_oom_abort_handler(mozalloc_oom_abort_handler handler)
michael@0 55 {
michael@0 56 gAbortHandler = handler;
michael@0 57 }

mercurial