alloc-r0drv-freebsd.c revision 1f1986470af9f0bb750dd859b142dc2e952deb20
4011N/A/* $Id$ */
4011N/A/** @file
4011N/A * innotek Portable Runtime - Memory Allocation, Ring-0 Driver, FreeBSD.
4011N/A */
4011N/A
4011N/A/*
4011N/A * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
4011N/A *
4011N/A * Permission is hereby granted, free of charge, to any person
4011N/A * obtaining a copy of this software and associated documentation
4011N/A * files (the "Software"), to deal in the Software without
4011N/A * restriction, including without limitation the rights to use,
4011N/A * copy, modify, merge, publish, distribute, sublicense, and/or sell
4011N/A * copies of the Software, and to permit persons to whom the
4011N/A * Software is furnished to do so, subject to the following
4011N/A * conditions:
4011N/A *
4011N/A * The above copyright notice and this permission notice shall be
4011N/A * included in all copies or substantial portions of the Software.
4011N/A *
4011N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4011N/A * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
4011N/A * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
4011N/A * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5031N/A * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
6106N/A * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
4011N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
4011N/A * OTHER DEALINGS IN THE SOFTWARE.
4011N/A */
4011N/A
4011N/A/*******************************************************************************
4011N/A* Header Files *
4011N/A*******************************************************************************/
4011N/A#include "the-freebsd-kernel.h"
6461N/A
4011N/A#include <iprt/alloc.h>
6349N/A#include <iprt/assert.h>
6106N/A#include <iprt/param.h>
4011N/A
4011N/A#include "r0drv/alloc-r0drv.h"
4011N/A
4011N/A
4011N/A/*******************************************************************************
6106N/A* Global Variables *
4011N/A*******************************************************************************/
6461N/A/* These two statements will define two globals and add initializers
5589N/A and destructors that will be called at load/unload time (I think). */
6461N/AMALLOC_DEFINE(M_IPRTHEAP, "iprtheap", "innotek Portable Runtime - heap");
4011N/AMALLOC_DEFINE(M_IPRTCONT, "iprtcont", "innotek Portable Runtime - contiguous");
6730N/A
4011N/A
6741N/APRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
4011N/A{
4011N/A PRTMEMHDR pHdr;
4011N/A Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
6106N/A
6747N/A /** @todo Just like OS/2, FreeBSD doesn't need this header. */
6747N/A pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
6741N/A fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
6741N/A if (pHdr)
6741N/A {
6741N/A pHdr->u32Magic = RTMEMHDR_MAGIC;
6741N/A pHdr->fFlags = fFlags;
4011N/A pHdr->cb = cb;
4011N/A pHdr->u32Padding = 0;
4011N/A return pHdr;
4011N/A }
6461N/A return NULL;
4011N/A}
4011N/A
4011N/A
4011N/Avoid rtMemFree(PRTMEMHDR pHdr)
4011N/A{
6113N/A pHdr->u32Magic += 1;
4011N/A free(pHdr, M_IPRTHEAP);
6747N/A}
4802N/A
4802N/A
4802N/ARTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
4802N/A{
4802N/A void *pv;
4802N/A
4802N/A /*
4802N/A * Validate input.
4802N/A */
4802N/A AssertPtr(pPhys);
4802N/A Assert(cb > 0);
4802N/A
4802N/A /*
4802N/A * This API works in pages, so no need to do any size aligning.
4802N/A */
4802N/A pv = contigmalloc(cb, /* size */
4802N/A M_IPRTCONT, /* type */
4802N/A M_NOWAIT | M_ZERO, /* flags */
4802N/A 0, /* lowest physical address*/
4802N/A _4G-1, /* highest physical address */
6747N/A PAGE_SIZE, /* alignment. */
6747N/A 0); /* boundrary */
6747N/A if (pv)
6747N/A {
6747N/A Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
4011N/A *pPhys = vtophys(pv);
4011N/A Assert(!(*pPhys & PAGE_OFFSET_MASK));
4011N/A }
4011N/A return pv;
4011N/A}
4011N/A
4066N/A
4011N/ARTR0DECL(void) RTMemContFree(void *pv, size_t cb)
6113N/A{
6113N/A if (pv)
6113N/A {
6113N/A AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
4011N/A contigfree(pv, cb, M_IPRTCONT);
6113N/A }
6113N/A}
6113N/A
6113N/A