shmget: make small free numbers reusable.

Change-Id: Ic6670214fa31a309e96794361e3ec2dcc6375f4a
Refs: #1531
This commit is contained in:
Tomoki Shirasawa
2020-09-01 07:26:02 +09:00
committed by Masamichi Takagi
parent b3ddd60277
commit e6e66e0392
3 changed files with 42 additions and 26 deletions

View File

@ -6161,9 +6161,40 @@ struct kshmid_ds {
struct list_head chain;
};
int the_maxi = -1;
unsigned long shmid_index[512];
static int get_shmid_max_index(void)
{
int i;
int index = -1;
for (i = 511; i >= 0; i--) {
if (shmid_index[i]) {
index = i * 64 + 63 - __builtin_clzl(shmid_index[i]);
break;
}
}
return index;
}
static int get_shmid_index(void)
{
int index = get_shmid_max_index();
int i;
unsigned long x;
for (index = 0;; index++) {
i = index / 64;
x = 1UL << (index % 64);
if (!(shmid_index[i] & x)) {
shmid_index[i] |= x;
break;
}
}
return index;
}
LIST_HEAD(kds_list);
LIST_HEAD(kds_free_list);
struct shminfo the_shminfo = {
.shmmax = 64L * 1024 * 1024 * 1024,
.shmmin = 1,
@ -6384,7 +6415,7 @@ int do_shmget(const key_t key, const size_t size, const int shmflg)
return error;
}
obj->index = ++the_maxi;
obj->index = get_shmid_index();
list_add(&obj->chain, &kds_list);
++the_shm_info.used_ids;
@ -6670,7 +6701,7 @@ SYSCALL_DECLARE(shmctl)
return error;
}
maxi = the_maxi;
maxi = get_shmid_max_index();
if (maxi < 0) {
maxi = 0;
}
@ -6777,7 +6808,7 @@ SYSCALL_DECLARE(shmctl)
return error;
}
maxi = the_maxi;
maxi = get_shmid_max_index();
if (maxi < 0) {
maxi = 0;
}