RMA 14.2 KB
Newer Older
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
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : RMA.c                                  */
/*                                                 */
/* Purpose: The browser needs to claim RMA space   */
/*          for some of the grottier protocols out */
/*          there now and again. There are a       */
/*          sufficiently large number of claims to */
/*          warrant a thin veneer over claim and   */
/*          release of RMA to keep track of blocks */
/*          and make sure leaks don't occur.       */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 24-Oct-97: Created.                    */
/***************************************************/

#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "swis.h"

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

#include "RMA.h"

/* Structures */

typedef struct rma_array_item
{
  unsigned int   flags;     /* Currently undefined (0) */
  browser_data * allocator;
  void         * rma_block;
}
rma_array_item;

/* Locals */

rma_array_item * rma_array          = NULL;
int              rma_array_elements = 0;

/* Static function prototypes */

static _kernel_oserror * rma_add_item    (int * new_item);
static _kernel_oserror * rma_remove_item (int item);

/*************************************************/
/* rma_claim()                                   */
/*                                               */
/* Claims a block of RMA, recording the usage by */
/* adding an item to the rma_array_item array.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             for which the claim is taking     */
/*             place, or NULL for a misc block - */
/*             in that case the block can only   */
/*             be identified for later freeing   */
/*             by its address;                   */
/*                                               */
/*             Size of the block to allocate, in */
/*             bytes;                            */
/*                                               */
/*             Pointer to a void * which will be */
/*             filled in with the address of the */
/*             new block.                        */
/*                                               */
/* Assumes:    The void * pointer is not NULL.   */
/*************************************************/

_kernel_oserror * rma_claim(browser_data * allocator, int size, void ** rma_block)
{
  _kernel_oserror * e;
  int               new_item;

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_claim: Called for allocator %p, block size %d\n",allocator,size);
  #endif

  if (rma_block) *rma_block = NULL;
  else return NULL;

  /* First add an array item to hold details on the RMA claim */

  RetError(rma_add_item(&new_item));

  /* The above should return an error if allocation fails, */
  /* but 'just in case'...                                 */

  if (new_item == -1) return make_no_memory_error(22);

  /* Fill it in, claiming the RMA space in passing */

  rma_array[new_item].flags     = 0;
  rma_array[new_item].allocator = allocator;

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_claim: \0211Allocating %d bytes RMA\0217\n",size);
  #endif

  e = _swix(OS_Module,
            _IN(0) | _IN(3) | _OUT(2),

            6,
            size,

            &rma_array[new_item].rma_block);

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_claim: \0211Been given block %p\0217\n",rma_array[new_item].rma_block);
  #endif

  /* If the claim appears to have failed, free the array item */
  /* again and return a generic out of memory error.          */

  if (!rma_array[new_item].rma_block)
  {
    rma_remove_item(new_item);
    return make_no_memory_error(22);
  }

  /* Otherwise, we've finished */

  if (rma_block) *rma_block = rma_array[new_item].rma_block;

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_claim: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* rma_release()                                 */
/*                                               */
/* Releases (frees) one or several blocks of RMA */
/* claimed by rma_claim, identified by block     */
/* address or an owning browser.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             for which the claim originally    */
/*             took place to free all blocks     */
/*             claimed for that browser - this   */
/*             is ignored if the second          */
/*             parameter is non-NULL (see        */
/*             below);                           */
/*                                               */
/*             Address of the block - this is    */
/*             always used in preference to the  */
/*             browser pointer, so it *must* be  */
/*             NULL if all blocks for a given    */
/*             browser are to be freed.          */
/*************************************************/

_kernel_oserror * rma_release(browser_data * allocator, void * rma_block)
{
  int search, found, found_any = 0;

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_release: Called for allocator %p, block %p\n",allocator,rma_block);
  #endif

  /* Sanity check */

  if (!allocator && !rma_block)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
               "Null allocator and RMA block pointer given to rma_release");

      return &erb;

    #endif

    return NULL;
  }

  /* If we've been given a specific block, search by that. Otherwise, */
  /* remove all items owned by the given browser.                     */

  if (rma_block)
  {
    do
    {
      found  = 0;
      search = 0;

      while (search < rma_array_elements)
      {
        if (rma_array[search].rma_block == rma_block)
        {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
          /* If an allocator was given, and the block doesn't */
          /* match it, then warn about this in TRACE builds.  */

          #ifdef TRACE

            if (allocator && rma_array[search].allocator != allocator)
            {
              erb.errnum = Utils_Error_Custom_Normal;

              sprintf(erb.errmess,
                      "Going to free block %p for browser %p, though the block's allocator was %p, in rma_release",
                      rma_block,
                      allocator,
                      rma_array[search].allocator);

              show_error_ret(&erb);
            }

          #endif

          /* Now free the block */

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
          rma_remove_item(search);
          found = found_any = 1;
          break;
        }

        search++;
      }
    }
    while (found);
  }
  else
  {
    do
    {
      found  = 0;
      search = 0;

      while (search < rma_array_elements)
      {
        if (rma_array[search].allocator == allocator)
        {
          rma_remove_item(search);
          found = found_any = 1;
          break;
        }

        search++;
      }
    }
    while (found);
  }

  /* Trace builds will complain if no item was found */
  /* - found_any isn't used for non-TRACE builds at  */
  /* the moment but could be in future, and so is    */
  /* unconditionally compiled in.                    */

  #ifdef TRACE

    if (!found_any)
    {
      erb.errnum = Utils_Error_Custom_Message;

      sprintf(erb.errmess,
              "Didn't find the item for allocator %p, address %p in rma_release",
              allocator,
              rma_block);

      show_error_ret(&erb);
    }

  #endif

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_release: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* rma_shutdown()                                */
/*                                               */
/* Frees all items in array of claimed blocks,   */
/* and all associated RMA blocks.                */
/*                                               */
/* TRACE builds will complain if there are any   */
/* items to free, as usually other processes     */
/* should have tidied up and this function       */
/* will have nothing to do except exit.          */
/*************************************************/

void rma_shutdown(void)
{
  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_shutdown: Called\n");
  #endif

  if (rma_array_elements)
  {
    int item;

    #ifdef TRACE

      if (tl & (1u<<12)) Printf("rma_shutdown: Have item(s) to free\n");

      erb.errnum = Utils_Error_Custom_Message;

      if (rma_array_elements == 1)
      {
        StrNCpy0(erb.errmess,
                 "Possible RMA leak; rma_shutdown has a block to free");
      }
      else
      {
        sprintf(erb.errmess,
                "Possible RMA leak; rma_shutdown has %d blocks to free",
                rma_array_elements);
      }

      show_error_ret(&erb);

    #endif

    /* Free all of the RMA blocks */

    for (item = 0; item < rma_array_elements; item++)
    {
      if (rma_array[item].rma_block)
      {
        _swix(OS_Module,
              _IN(0) | _IN(2),

              7,
              rma_array[item].rma_block);
      }
    }

    /* Free the array itself */

    free(rma_array);

    rma_array          = NULL;
    rma_array_elements = 0;
  }

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_shutdown: Successful\n");
  #endif

  return;
}

/*************************************************/
/* rma_add_item()                                */
/*                                               */
/* Adds an rma_array_item struct to the array of */
/* such structures. The contents are not         */
/* initialised.                                  */
/*                                               */
/* Parameters: Pointer to an int, in which the   */
/*             array index of the new item is    */
/*             placed, or -1 for an error - this */
/*             will be in addition to an error   */
/*             returned directly.                */
/*                                               */
/* Assumes:    The pointer may not be NULL.      */
/*************************************************/

static _kernel_oserror * rma_add_item(int * new_item)
{
  rma_array_item * new_array;

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_add_item: Called\n");
  #endif

  if (!new_item)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      StrNCpy0(erb.errmess,
               "Null new_item int pointer passed to rma_add_item");

      return &erb;

    #endif

    return NULL;
  }

  if (!rma_array_elements) new_array = malloc (sizeof(rma_array_item));
  else                     new_array = realloc(rma_array, sizeof(rma_array_item) * (rma_array_elements + 1));

  if (!new_array) return make_no_memory_error(22);
  else rma_array = new_array;

  *new_item = rma_array_elements++; /* (Post-increment as we want to return an array index, not how many elements there are) */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_add_item: Successful, item count is now %d\n", rma_array_elements);
  #endif

  return NULL;
}

/*************************************************/
/* rma_remove_item()                             */
/*                                               */
/* Removes an rma_array_item from the array of   */
/* such structures.                              */
/*                                               */
/* Parameters: Index into the array of the item  */
/*             to remove.                        */
/*************************************************/

static _kernel_oserror * rma_remove_item(int item)
{
436
  rma_array_item * new_array;
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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_remove_item: Called for item %d\n",item);
  #endif

  /* Limit check 'item' */

  if (item >= rma_array_elements)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid item number (%d, with %d elements) passed to rma_remove_item",
              item,
              rma_array_elements);

      return &erb;

    #endif

    return NULL;
  }

  /* Is there an associated RMA block to free? If so, */
  /* free it!                                         */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_remove_item: \0212Freeing RMA block %p\0217\n",rma_array[item].rma_block);
  #endif

  if (rma_array[item].rma_block)
  {
    RetError(_swix(OS_Module,
                   _IN(0) | _IN(2),

                   7,
                   rma_array[item].rma_block));

    rma_array[item].rma_block = NULL;
  }

  /* Are we removing the only item? If so, just free the array */

  if (rma_array_elements == 1)
  {
    #ifdef TRACE
      if (tl & (1u<<12)) Printf("rma_remove_item: This is the last item, so freeing the array\n");
    #endif

    free(rma_array);

    rma_array          = NULL;
    rma_array_elements = 0;
  }
  else
  {
    /* Are we removing the last item? If not, must shuffle */
    /* the other elements down a bit.                      */

    if (item != rma_array_elements - 1)
    {
      #ifdef TRACE
        if (tl & (1u<<12)) Printf("rma_remove_item: This is not the head item, so shuffling higher items down\n");
      #endif

      memmove(&rma_array[item],
              &rma_array[item + 1],
              sizeof(rma_array_item) * (rma_array_elements - (item + 1)));
    }

    /* Now shrink the array */

    new_array = realloc(rma_array, sizeof(rma_array_item) * (rma_array_elements - 1));

    if (new_array) rma_array = new_array, rma_array_elements--;
  }

  /* Finished. */

  #ifdef TRACE
    if (tl & (1u<<12)) Printf("rma_remove_item: Successful, item count is now %d\n", rma_array_elements);
  #endif

  return NULL;
}