1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
/* Copyright (c) 1996-2004, Adaptec Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Adaptec Corporation nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* File - SHARMEM.C */
/*****************************************************************************/
/* */
/*Description: */
/* */
/* */
/* */
/* */
/*Author: Bob Pasteur */
/*Date: */
/* */
/*Editors: */
/* */
/*Remarks: */
/* */
/* */
/*****************************************************************************/
/*Include Files -------------------------------------------------------------*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <osd_util.h>
#include <comm_eng.hpp>
#include <commlist.hpp>
#include <osd_unix.h>
#include <sharmem.h>
/* Definitions - Defines & Constants ---------------------------------------*/
/* Definitions - Structure & Typedef ---------------------------------------*/
/* Variables - External ----------------------------------------------------*/
/* Variables - Global ------------------------------------------------------*/
static int BufferID = -1;
static char *SharedMemoryPtr;
extern uSHORT dsplyFlags;
#define DSPLY_SHAR_MEM 0x0004
/*-------------------------------------------------------------------------*/
/* Function GetSharedMemory */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed Into This Function */
/* */
/* This Function Will Allocate The Shared Memory */
/* */
/* Return : Pointer To The Attached Shared Memory, 0 If Failure */
/*-------------------------------------------------------------------------*/
static char *GetSharedMemory(void)
{
struct shmid_ds shm_buff;
uLONG TotalSize;
char *Ptr;
ShMemHdr *Header;
Ptr = NULL;
/* Calculate The Size We Need */
TotalSize = sizeof(DPTCE_MasterMod_C) * MaxCommModules *
CommModuleMultiplier +
sizeof(DPTCE_RemoteInEP_C) * MaxRemoteConnections +
sizeof(DPTCE_InputEP_C) * MaxDirectInputConnections +
sizeof(DPTCE_Base_C) * MaxGlobalLists +
WorkingSize;
if(TotalSize < MinAllocationSize)
TotalSize = MinAllocationSize;
/* Get The Shared Memory Segment */
BufferID = shmget(IPC_PRIVATE,(int)TotalSize,
SHM_ALLRD | SHM_ALLWR | IPC_CREAT);
/* If We Got The Segment, Try To Attach To It */
if(BufferID != -1)
{
Ptr = (char *)shmat(BufferID,0,0);
/* The Attach Failed, So DeAllocate The Shared Memory */
if((int)Ptr == -1)
{
shmctl(BufferID,IPC_RMID,&shm_buff);
BufferID = -1;
}
/* The Attach Was Successful, So Set Up The Memory Header And */
/* Set It Up As One Big Element In The List */
else {
Header = (ShMemHdr *)Ptr;
Header->Busy = 0;
Header->Size = TotalSize;
Header->FreeList = (ShMemElmt *)(&Header->FreeSpace);
Header->FreeList->Prev = NULL;
Header->FreeList->Next = NULL;
Header->FreeList->Size = TotalSize - sizeof(ShMemElmt) -
sizeof(ShMemHdr);
}
}
return(Ptr);
}
/*-------------------------------------------------------------------------*/
/* Function FindBestFit */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are : */
/* FreeList : List Of Elements To Be Searched */
/* Size : Size In Bytes To Be Searched For */
/* */
/* This Function Will Search The Passed In List For An Enty That Will Best */
/* Fit The Passed In Size */
/* */
/* Return : Pointer To The Best Fit Element Found, Still Linked Into The */
/* List */
/*-------------------------------------------------------------------------*/
static ShMemElmt *FindBestFit(ShMemElmt *FreeList,uLONG Size)
{
ShMemElmt *BestFit;
BestFit = NULL;
/* Search All Elements */
while(FreeList != NULL)
{
/* If The Size Asked For Is Exact, We Are Done */
if(FreeList->Size == Size)
{
BestFit = FreeList;
break;
}
/* If The Size Of The List Element Is Greater Than Our Size, This Is */
/* A Posibility */
else if(FreeList->Size > Size)
{
/* If We Haven't Found Any Others Yet, Save Him Off And Move On */
if(BestFit == NULL)
BestFit = FreeList;
/* If We Have One Already, See If This One Is Better */
else {
if((FreeList->Size < BestFit->Size)&&
(FreeList->Size - Size < MinUnitAllocation))
BestFit = FreeList;
}
}
FreeList = FreeList->Next;
}
return(BestFit);
}
/*-------------------------------------------------------------------------*/
/* Function RemoveFromList */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are : */
/* Header : Pointer To The Main Memory Header */
/* BestFit : Pointer To The Element To Remove From The List */
/* Size : Number Of Bytes Being Asked For */
/* */
/* This Function */
/* */
/* Return : RNONE */
/*-------------------------------------------------------------------------*/
static void RemoveFromList(ShMemHdr *Header, ShMemElmt *BestFit, uLONG Size)
{
ShMemElmt *NewElement;
char *Ptr;
uLONG SizeLeft;
/* If The Size Asked For Is only Minimally Smaller Than The Actual */
/* Size, Just Pull It From The List As Is */
if(BestFit->Size - Size < MinUnitAllocation + sizeof(ShMemElmt))
{
/* This One Is The Head Of The List */
if(BestFit->Prev == NULL)
{
Header->FreeList = BestFit->Next;
if(Header->FreeList != NULL)
Header->FreeList->Prev = NULL;
}
/* Not The Head Of The List */
else {
BestFit->Prev->Next = BestFit->Next;
if(BestFit->Next != NULL)
BestFit->Next->Prev = BestFit->Prev;
}
}
/* The Size Being Asked For Is Much Smaller Than The Actual Size, */
/* So We Are Going To Break It Into Two Headers, One To Keep And */
/* One To Share */
else {
if(Size < MinUnitAllocation)
Size = MinUnitAllocation;
/* Calculate The Remaing Size And Set Up The Size For The One */
/* We Are Releasing (BestFit) */
SizeLeft = BestFit->Size - Size - sizeof(ShMemElmt);
BestFit->Size = Size;
/* The NewElement Will Be The One We Keep In Our List, So Set It Up */
Ptr = (char *)BestFit + sizeof(ShMemElmt);
NewElement = (ShMemElmt *)(Ptr + Size);
NewElement->Size = SizeLeft;
NewElement->Prev = BestFit->Prev;
NewElement->Next = BestFit->Next;
/* If This Is The First Element In The List, Bump It To The New Element */
if(Header->FreeList == BestFit)
Header->FreeList = NewElement;
}
BestFit->Prev = BestFit->Next = NULL;
}
/*-------------------------------------------------------------------------*/
/* Function FreeSharedMemory */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed In To This Function */
/* */
/* This Function Will DeAllocate The Shared Memory Buffer */
/* */
/* Return : None */
/*-------------------------------------------------------------------------*/
void CommFreeSharedMemory(void)
{
struct shmid_ds shm_buff;
/* Make Sure That It Was Allocated First */
if(BufferID != -1)
{
/* UnAttach The Memory If It Is Attached */
if(SharedMemoryPtr != NULL)
shmdt(SharedMemoryPtr);
/* Free The Segment */
shmctl(BufferID,IPC_RMID,&shm_buff);
}
SharedMemoryPtr = NULL;
BufferID = -1;
}
/*-------------------------------------------------------------------------*/
/* Function AddToList */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are : */
/* Header : Pointer To The Main Memory Header */
/* Element : Pointer To The Element To Be Added Back Into The List */
/* */
/* This Function Will Add An Element Into The Free List, Combining It With */
/* Another Element If Possible */
/* */
/* Return : NONE */
/*-------------------------------------------------------------------------*/
static void AddToList(ShMemHdr *Header, ShMemElmt *Element)
{
char *Ptr;
ShMemElmt *FreeList;
/* If The Free List Is Empty, Make Him The First And Exit */
FreeList = Header->FreeList;
if(FreeList == NULL)
{
Header->FreeList = Element;
}
/* The List Is Not Empty, So First Find His Place In The List */
else {
while(FreeList->Next != NULL)
{
if(FreeList > Element)
break;
FreeList = FreeList->Next;
}
/* This Element Will Insert Before Our Free List Element */
if(Element < FreeList)
{
/* If The FreeList Is The First Element In The List, Make The New Element */
/* The First Element In The List */
if(Header->FreeList == FreeList)
Header->FreeList = Element;
/* If These Elements Will Combine, Add FreeList Into The Element */
Ptr = (char *)Element;
if((Ptr + Element->Size + sizeof(ShMemElmt)) ==
(char *)FreeList)
{
Element->Size += FreeList->Size + sizeof(ShMemElmt);
Element->Next = FreeList->Next;
Element->Prev = FreeList->Prev;
}
/* They Will Not Combine, So Insert The Element Before FreeList */
else {
Element->Next = FreeList;
Element->Prev = FreeList->Prev;
if(Element->Prev != NULL)
Element->Prev->Next = Element;
FreeList->Prev = Element;
}
}
/* This Element Will Insert After Our Free List Element */
else {
/* If These Elements Will Combine, Add The Element Into FreeList */
Ptr = (char *)FreeList;
if((Ptr + FreeList->Size + sizeof(ShMemElmt)) ==
(char *)Element)
{
FreeList->Size += Element->Size + sizeof(ShMemElmt);
}
/* They Will Not Combine, So Insert The Element After FreeList */
else {
Element->Prev = FreeList;
Element->Next = FreeList->Next;
FreeList->Next = Element;
if(Element->Next != NULL)
Element->Next->Prev = Element;
}
}
}
}
/*-------------------------------------------------------------------------*/
/* Function AllocBytes */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are : */
/* Size : The Number Of Bytes To Allocate */
/* */
/* This Function Will Allocate Memory From A Shared Memory Pool */
/* */
/* Return : Pointer To The Shared Memory, NULL If Failure */
/*-------------------------------------------------------------------------*/
char *AllocBytes(uLONG Size)
{
char *Ptr;
ShMemHdr *Header;
ShMemElmt *FreeList;
ShMemElmt *BestFit;
Ptr = NULL;
/*If Not Set Up, Go Set Up The Shared Memory Segment */
if(BufferID == -1)
{
SharedMemoryPtr = GetSharedMemory();
if(SharedMemoryPtr == NULL)
return(NULL);
}
/* Grab The Shared Memory Header In The Shared Memory */
Header = (ShMemHdr *)SharedMemoryPtr;
/* Wait For The Busy Flag To Go Down */
while(Header->Busy)
;
Header->Busy = 1;
/* Grab The Free List, And Go Find A Suitable Element */
FreeList = Header->FreeList;
BestFit = FindBestFit(FreeList,Size);
/* If We Found One, Remove It From The List */
if(BestFit != NULL)
{
RemoveFromList(Header,BestFit,Size);
Ptr = (char *)BestFit + sizeof(ShMemElmt);
}
Header->Busy = 0;
#ifdef __INC_SCREEN_IO
if(dsplyFlags & DSPLY_SHAR_MEM)
printf("AllocBytes : %lx At Address %08lxh \n",Size,Ptr);
#endif
return(Ptr);
}
/*-------------------------------------------------------------------------*/
/* Function FreeBytes */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are : */
/* MemPtr : Pointer To The Shared Memory To Be Freed */
/* */
/* This Function Will Return An Allocated Element Of Shared Memory Back */
/* To The Pool */
/* */
/* Return : NONE */
/*-------------------------------------------------------------------------*/
void FreeBytes(char *MemPtr)
{
ShMemHdr *Header;
ShMemElmt *Element;
Header = (ShMemHdr *)SharedMemoryPtr;
while(Header->Busy)
;
Header->Busy = 1;
Element = (ShMemElmt *)(MemPtr - sizeof(ShMemElmt));
#ifdef __INC_SCREEN_IO
if(dsplyFlags & DSPLY_SHAR_MEM)
printf("FreeBytes : %lx At Address %08lxh \n",
Element->Size,MemPtr);
#endif
/* Do A Simple Check To See If This Element Is Within Our Memory Range */
if((MemPtr > SharedMemoryPtr)&&(MemPtr - SharedMemoryPtr < Header->Size))
AddToList(Header,Element);
Header->Busy = 0;
}
|