فهرست منبع

Make the other structs static

The next series of commits are for memory allocation (allocating only the variables/modules needed before each view and then deallocating after the view)
jblanked 1 سال پیش
والد
کامیت
5296eb40be
4فایلهای تغییر یافته به همراه6 افزوده شده و 37 حذف شده
  1. 0 12
      explore/flip_social_explore.c
  2. 2 2
      flip_social.h
  3. 3 10
      friends/flip_social_friends.c
  4. 1 13
      messages/flip_social_messages.c

+ 0 - 12
explore/flip_social_explore.c

@@ -22,18 +22,6 @@ FlipSocialModel *flip_social_explore_alloc(void)
         FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
         FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
         return NULL;
         return NULL;
     }
     }
-    for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
-    {
-        if (explore->usernames[i] == NULL)
-        {
-            explore->usernames[i] = malloc(MAX_USER_LENGTH);
-            if (explore->usernames[i] == NULL)
-            {
-                FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
-                return NULL; // Return false on memory allocation failure
-            }
-        }
-    }
     return explore;
     return explore;
 }
 }
 
 

+ 2 - 2
flip_social.h

@@ -82,14 +82,14 @@ typedef struct
 
 
 typedef struct
 typedef struct
 {
 {
-    char *usernames[MAX_EXPLORE_USERS];
+    char usernames[MAX_EXPLORE_USERS][MAX_USER_LENGTH];
     int count;
     int count;
     int index;
     int index;
 } FlipSocialModel;
 } FlipSocialModel;
 
 
 typedef struct
 typedef struct
 {
 {
-    char *usernames[MAX_MESSAGE_USERS];
+    char usernames[MAX_MESSAGE_USERS][MAX_USER_LENGTH];
     int count;
     int count;
     int index;
     int index;
 } FlipSocialModel2;
 } FlipSocialModel2;

+ 3 - 10
friends/flip_social_friends.c

@@ -4,17 +4,10 @@ FlipSocialModel *flip_social_friends_alloc()
 {
 {
     // Allocate memory for each username only if not already allocated
     // Allocate memory for each username only if not already allocated
     FlipSocialModel *friends = malloc(sizeof(FlipSocialModel));
     FlipSocialModel *friends = malloc(sizeof(FlipSocialModel));
-    for (size_t i = 0; i < MAX_FRIENDS; i++)
+    if (friends == NULL)
     {
     {
-        if (friends->usernames[i] == NULL)
-        {
-            friends->usernames[i] = malloc(MAX_USER_LENGTH);
-            if (friends->usernames[i] == NULL)
-            {
-                FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
-                return NULL; // Return false on memory allocation failure
-            }
-        }
+        FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
+        return NULL;
     }
     }
     return friends;
     return friends;
 }
 }

+ 1 - 13
messages/flip_social_messages.c

@@ -16,18 +16,6 @@ FlipSocialModel2 *flip_social_messages_alloc()
         FURI_LOG_E(TAG, "Failed to allocate memory for message users");
         FURI_LOG_E(TAG, "Failed to allocate memory for message users");
         return NULL;
         return NULL;
     }
     }
-    for (size_t i = 0; i < MAX_MESSAGE_USERS; i++)
-    {
-        if (users->usernames[i] == NULL)
-        {
-            users->usernames[i] = malloc(MAX_USER_LENGTH);
-            if (users->usernames[i] == NULL)
-            {
-                FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
-                return NULL; // Return false on memory allocation failure
-            }
-        }
-    }
     return users;
     return users;
 }
 }
 
 
@@ -157,7 +145,7 @@ bool flip_social_get_messages_with_user()
         FURI_LOG_E(TAG, "Username is NULL");
         FURI_LOG_E(TAG, "Username is NULL");
         return false;
         return false;
     }
     }
-    if (!flip_social_message_users->usernames[flip_social_message_users->index] || strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
+    if (strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
     {
     {
         FURI_LOG_E(TAG, "Username is NULL");
         FURI_LOG_E(TAG, "Username is NULL");
         return false;
         return false;