# SFBHeap.h

    00001 /*                                             -*- mode:C++; fill-column:100 -*-
    00002   SFBHeap.cpp - The ancient K&R malloc, tweaked for packets
    00003   Copyright (C) 2009 The Regents of the University of New Mexico.  All rights reserved.
    00004
    00005   This library is free software; you can redistribute it and/or
    00006   modify it under the terms of the GNU Lesser General Public
    00007   License as published by the Free Software Foundation; either
    00008   version 2.1 of the License, or (at your option) any later version.
    00009
    00010   This library is distributed in the hope that it will be useful,
    00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013   Lesser General Public License for more details.
    00014
    00015   You should have received a copy of the GNU General Public License
    00016   along with this library; if not, write to the Free Software
    00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00018   USA
    00019
    00020   $Id$
    00021 */
    00022
    00023 #ifndef SFBHEAP_H
    00024 #define SFBHEAP_H
    00025
    00026 #include "SFBFrame.h"
    00027 #include "SFBReactor.h"  /* For PacketHandler, ::reflex */
    00028
    00043 class SFBHeap {
    00044
    00045 public:
    00046
    00047   SFBHeap() ;
    00048
    00049   void * malloc(u32 bytes) ;
    00050   void free(void * packet) ;
    00051
    00052   u8 * remember(const u8 * packet, bool unreadOnly = false, u8 sourceOverride = MAX_FACE_INDEX) ;
    00053   u8 * rememberBytes(const u8 * bytePtr, u32 byteCount) ;
    00054   void forget(u8 * packet) ;
    00055
    00056   u8 * makePacket(u32 size) ;
    00057
    00058   enum {
    00059     HEAP_SIZE_HEADERS = 1<<8,
    00060     HEAP_SIZE_BYTES = HEAP_SIZE_HEADERS<<3,
    00061     FREE_CHECK_BYTES = 0x5aa5,
    00062     USED_CHECK_BYTES = 0xaa55
    00063   };
    00064
    00065 private:
    00066
    00067   union HeapHeader {
    00068     double forAlignmentOnly;
    00069     struct {
    00070       u16 checkBytes;
    00071       u16 size;                   /* in HeapHeader's, so 512KB max heap size */
    00072       union {
    00073         PacketHeader ph;
    00074         HeapHeader * nextFree;
    00075       } s;
    00076     } f;
    00077   };
    00078   static void * initUsed(HeapHeader * h, u32 words) ;
    00079
    00080   static u32 dataSize(HeapHeader * h) ;
    00081
    00082   static bool putPacketByte(u8 * packet, const u8 byte) ;
    00083
    00084   static void terminatePacket(u8 * packet) ;
    00085
    00086   static u32 headers(u32 packetBytes) ;
    00087   HeapHeader * findFit(u32 size) ;
    00088
    00089   HeapHeader base;
    00090   HeapHeader theHeap[HEAP_SIZE_HEADERS];
    00091   HeapHeader * freePtr;
    00092
    00093   HeapHeader * asHeader(u32 index) {
    00094     return (HeapHeader*) &theHeap[index];
    00095   }
    00096
    00097 #ifdef TEST_SFBHEAP
    00098   friend class TestSFBHeap;
    00099 #endif
    00100
    00101 };
    00102
    00103 #endif  /* SFBHEAP_H */
    00104