coverity appeasement - redundant check
[minix.git] / servers / vm / pb.c
blobb7434c2e0ced26b6ee85d7dd4cc300164cb19000
2 #define _SYSTEM 1
4 #include <minix/com.h>
5 #include <minix/callnr.h>
6 #include <minix/type.h>
7 #include <minix/config.h>
8 #include <minix/const.h>
9 #include <minix/sysutil.h>
10 #include <minix/syslib.h>
11 #include <minix/debug.h>
12 #include <minix/bitmap.h>
13 #include <minix/hash.h>
15 #include <sys/mman.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <assert.h>
21 #include <stdint.h>
22 #include <memory.h>
23 #include <sys/param.h>
25 #include "vm.h"
26 #include "proto.h"
27 #include "util.h"
28 #include "glo.h"
29 #include "region.h"
30 #include "sanitycheck.h"
31 #include "physravl.h"
32 #include "memlist.h"
34 struct phys_block *pb_new(phys_bytes phys)
36 struct phys_block *newpb;
38 if(!SLABALLOC(newpb)) {
39 printf("vm: pb_new: couldn't allocate phys block\n");
40 return NULL;
43 assert(!(phys % VM_PAGE_SIZE));
44 assert(phys != MAP_NONE);
46 USE(newpb,
47 newpb->phys = phys;
48 newpb->refcount = 0;
49 newpb->firstregion = NULL;
52 return newpb;
55 struct phys_region *pb_reference(struct phys_block *newpb, vir_bytes offset, struct vir_region *region)
57 struct phys_region *newphysr;
59 if(!SLABALLOC(newphysr)) {
60 printf("vm: pb_reference: couldn't allocate phys region\n");
61 return NULL;
64 /* New physical region. */
65 USE(newphysr,
66 newphysr->offset = offset;
67 newphysr->ph = newpb;
68 newphysr->parent = region;
69 newphysr->next_ph_list = newpb->firstregion;
70 newpb->firstregion = newphysr;);
72 newpb->refcount++;
73 physr_insert(region->phys, newphysr);
75 return newphysr;
78 /*===========================================================================*
79 * pb_unreferenced *
80 *===========================================================================*/
81 void pb_unreferenced(struct vir_region *region, struct phys_region *pr, int rm)
83 struct phys_block *pb;
85 pb = pr->ph;
86 assert(pb->refcount > 0);
87 USE(pb, pb->refcount--;);
88 assert(pb->refcount >= 0);
90 if(pb->firstregion == pr) {
91 USE(pb, pb->firstregion = pr->next_ph_list;);
92 } else {
93 struct phys_region *others;
95 for(others = pb->firstregion; others;
96 others = others->next_ph_list) {
97 assert(others->ph == pb);
98 if(others->next_ph_list == pr) {
99 USE(others, others->next_ph_list = pr->next_ph_list;);
100 break;
104 assert(others); /* Otherwise, wasn't on the list. */
107 if(pb->refcount == 0) {
108 assert(!pb->firstregion);
109 if(region->flags & VR_ANON) {
110 free_mem(ABS2CLICK(pb->phys), 1);
111 } else if(region->flags & VR_DIRECT) {
112 ; /* No action required. */
113 } else {
114 panic("strange phys flags");
116 SLABFREE(pb);
119 if(rm) physr_remove(region->phys, pr->offset);