isindfreel.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isindfreel.c /main/3 1995/10/23 11:41:03 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isfreelist.c
  33. *
  34. * Description:
  35. * Free list maintenance functions
  36. */
  37. #include "isam_impl.h"
  38. extern Bufhdr *_isdisk_fix();
  39. /*
  40. * blkno = _isfreel_alloc()
  41. *
  42. * Allocate a new index page.
  43. */
  44. Blkno
  45. _isindfreel_alloc(Fcb *fcb)
  46. {
  47. Bufhdr *pbhdr;
  48. char *p;
  49. int npointers;
  50. Blkno blkno;
  51. if (fcb->indfreelist == FREELIST_NOPAGE) {
  52. /*
  53. * We must write something to the buffer, or we will get
  54. * segmentation fault when using mapped I/O.
  55. */
  56. fcb->indsize = _extend_file(fcb, fcb->indfd, fcb->indsize);
  57. return (fcb->indsize - 1);
  58. }
  59. pbhdr = _isdisk_fix(fcb, fcb->indfd, fcb->indfreelist, ISFIXWRITE);
  60. p = pbhdr->isb_buffer;
  61. npointers = ldshort(p + FL_NPOINTERS_OFF);
  62. if (npointers > 0) {
  63. blkno = ldblkno(p + FL_POINTERS_OFF + npointers * BLKNOSIZE);
  64. npointers--;
  65. stshort((short)npointers, p + FL_NPOINTERS_OFF);
  66. return (ldblkno(p + FL_POINTERS_OFF + npointers * BLKNOSIZE));
  67. }
  68. else {
  69. blkno = fcb->indfreelist;
  70. fcb->indfreelist = ldblkno(p + FL_NEXT_OFF);
  71. return (blkno);
  72. }
  73. }
  74. /*
  75. * _isfreel_free()
  76. *
  77. * Free an index page.
  78. */
  79. void
  80. _isindfreel_free(Fcb *fcb, Blkno blkno)
  81. {
  82. Bufhdr *pbhdr;
  83. char *p;
  84. int npointers;
  85. if (fcb->indfreelist != FREELIST_NOPAGE) {
  86. pbhdr = _isdisk_fix(fcb, fcb->indfd, fcb->indfreelist, ISFIXWRITE);
  87. p = pbhdr->isb_buffer;
  88. npointers = ldshort(p + FL_NPOINTERS_OFF);
  89. if (npointers < FL_MAXNPOINTERS) {
  90. stblkno(blkno, p + FL_POINTERS_OFF + npointers * BLKNOSIZE);
  91. npointers++;
  92. stshort((short)npointers, p + FL_NPOINTERS_OFF);
  93. return;
  94. }
  95. }
  96. pbhdr = _isdisk_fix(fcb, fcb->indfd, blkno, ISFIXWRITE);
  97. p = pbhdr->isb_buffer;
  98. /* Mark page to indicate that it is in the free list. */
  99. stshort((short)PT_FREELIST, p + FL_TYPE_OFF);
  100. stshort((short)0, p + FL_NPOINTERS_OFF);
  101. stblkno(fcb->indfreelist, p + FL_NEXT_OFF);
  102. fcb->indfreelist = blkno;
  103. }