2500: #
2501: /*
2502: */
2503:
2504: /*
2505: * Structure of the coremap and swapmap
2506: * arrays. Consists of non-zero count
2507: * and base address of that many
2508: * contiguous units.
2509: * (The coremap unit is 64 bytes,
2510: * the swapmap unit is 512 bytes)
2511: * The addresses are increasing and
2512: * the list is terminated with the
2513: * first zero count.
2514: */
2515: struct map
2516: {
2517: char *m_size;
2518: char *m_addr;
2519: };
2520: /* --------------------------- */
2521:
2522: /*
2523: * Allocate size units from the given
2524: * map. Return the base of the allocated
2525: * space.
2526: * Algorithm is first fit.
2527: */
2528: malloc(mp, size)
2529: struct map *mp;
2530: {
2531: register int a;
2532: register struct map *bp;
2533:
2534: for (bp = mp; bp->m_size; bp++) {
2535: if (bp->m_size >= size) {
2536: a = bp->m_addr;
2537: bp->m_addr =+ size;
2538: if ((bp->m_size =- size) == 0)
2539: do {
2540: bp++;
2541: (bp-1)->m_addr = bp->m_addr;
2542: } while ((bp-1)->m_size = bp->m_size);
2543: return(a);
2544: }
2545: }
2546: return(0);
2547: }
2548: /* --------------------------- */
2549:
2550: /*
2551: * Free the previously allocated space aa
2552: * of size units into the specified map.
2553: * Sort aa into map and combine on
2554: * one or both ends if possible.
2555: */
2556: mfree(mp, size, aa)
2557: struct map *mp;
2558: {
2559: register struct map *bp;
2560: register int t;
2561: register int a;
2562:
2563: a = aa;
2564: for (bp = mp; bp->m_addr<=a && bp->m_size!=0; bp++);
2565: if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) {
2566: (bp-1)->m_size =+ size;
2567: if (a+size == bp->m_addr) {
2568: (bp-1)->m_size =+ bp->m_size;
2569: while (bp->m_size) {
2570: bp++;
2571: (bp-1)->m_addr = bp->m_addr;
2572: (bp-1)->m_size = bp->m_size;
2573: }
2574: }
2575: } else {
2576: if (a+size == bp->m_addr && bp->m_size) {
2577: bp->m_addr =- size;
2578: bp->m_size =+ size;
2579: } else if (size) do {
2580: t = bp->m_addr;
2581: bp->m_addr = a;
2582: a = t;
2583: t = bp->m_size;
2584: bp->m_size = size;
2585: bp++;
2586: } while (size = t);
2587: }
2588: }
2589: /* --------------------------- */