g_lock: simplify g_lock_trylock
authorVolker Lendecke <vl@samba.org>
Wed, 28 Jun 2017 17:12:36 +0000 (19:12 +0200)
committerRalph Boehme <slow@samba.org>
Tue, 25 Jul 2017 15:43:17 +0000 (17:43 +0200)
The now mandatory talloc_realloc_array will go away soon

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/lib/g_lock.c

index a22ec94c9c8fdadb717a304769e14bc204d05b18..e93ef3fc82b3f028078df43650152f24ffe0d07d 100644 (file)
@@ -257,7 +257,7 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
                               struct server_id *blocker)
 {
        TDB_DATA data, userdata;
-       size_t i, num_locks, my_lock;
+       size_t i, num_locks;
        struct g_lock_rec *locks, *tmp;
        NTSTATUS status;
        bool modified = false;
@@ -270,8 +270,6 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
                return status;
        }
 
-       my_lock = num_locks;    /* doesn't exist yet */
-
        if ((type == G_LOCK_READ) && (num_locks > 0)) {
                /*
                 * Read locks can stay around forever if the process
@@ -296,7 +294,12 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
                                status = NT_STATUS_WAS_LOCKED;
                                goto done;
                        }
-                       my_lock = i;
+                       /*
+                        * Remove "our" lock entry. Re-add it later
+                        * with our new lock type.
+                        */
+                       locks[i] = locks[num_locks-1];
+                       num_locks -= 1;
                        continue;
                }
 
@@ -325,19 +328,19 @@ static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
                }
        }
 
-       if (my_lock >= num_locks) {
-               tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
-                                    num_locks+1);
-               if (tmp == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto done;
-               }
-               locks = tmp;
-               my_lock = num_locks;
-               num_locks += 1;
+       tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
+                            num_locks+1);
+       if (tmp == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
        }
+       locks = tmp;
+
+       locks[num_locks] = (struct g_lock_rec) {
+               .pid = self, .lock_type = type
+       };
+       num_locks +=1 ;
 
-       locks[my_lock] = (struct g_lock_rec){ .pid = self, .lock_type = type };
        modified = true;
 
        status = NT_STATUS_OK;