YaGL: Replaced g_malloc/g_free with preallocated buffer
authorStanislav Vorobiov <s.vorobiov@samsung.com>
Mon, 17 Sep 2012 10:30:31 +0000 (14:30 +0400)
committerEvgeny Voevodin <e.voevodin@samsung.com>
Tue, 18 Sep 2012 06:19:23 +0000 (10:19 +0400)
hw/yagl_apis/gles/yagl_gles_context.c
hw/yagl_apis/gles/yagl_gles_context.h
hw/yagl_apis/gles/yagl_host_gles_calls.c
hw/yagl_apis/gles2/yagl_host_gles2_calls.c

index 9ed1ae2..7c19536 100644 (file)
@@ -171,6 +171,9 @@ void yagl_gles_context_init(struct yagl_gles_context *ctx,
 
     ctx->driver_ps = driver_ps;
 
+    ctx->malloc_buff_size = 100;
+    ctx->malloc_buff = g_malloc(ctx->malloc_buff_size);
+
     ctx->error = GL_NO_ERROR;
 
     ctx->arrays = NULL;
@@ -288,6 +291,9 @@ void yagl_gles_context_cleanup(struct yagl_gles_context *ctx)
 
     g_free(ctx->arrays);
     ctx->arrays = NULL;
+
+    g_free(ctx->malloc_buff);
+    ctx->malloc_buff = NULL;
 }
 
 void yagl_gles_context_set_error(struct yagl_gles_context *ctx, GLenum error)
@@ -306,6 +312,26 @@ GLenum yagl_gles_context_get_error(struct yagl_gles_context *ctx)
     return error;
 }
 
+void *yagl_gles_context_malloc(struct yagl_gles_context *ctx, GLsizei size)
+{
+    if (size > ctx->malloc_buff_size) {
+        ctx->malloc_buff_size = size;
+        g_free(ctx->malloc_buff);
+        ctx->malloc_buff = g_malloc(ctx->malloc_buff_size);
+    }
+
+    return ctx->malloc_buff;
+}
+
+void *yagl_gles_context_malloc0(struct yagl_gles_context *ctx, GLsizei size)
+{
+    void *tmp = yagl_gles_context_malloc(ctx, size);
+
+    memset(tmp, 0, size);
+
+    return tmp;
+}
+
 struct yagl_gles_array
     *yagl_gles_context_get_array(struct yagl_gles_context *ctx, GLuint index)
 {
index 57df311..8f63737 100644 (file)
@@ -35,6 +35,14 @@ struct yagl_gles_context
     GLenum error;
 
     /*
+     * Buffer that is used for reading target data that is
+     * accessed via target pointers. Instead of doing
+     * g_malloc/g_free every time we'll just use this.
+     */
+    void *malloc_buff;
+    GLsizei malloc_buff_size;
+
+    /*
      * GLES arrays, the number of arrays is different depending on
      * GLES version, 'num_arrays' holds that number.
      */
@@ -116,6 +124,12 @@ void yagl_gles_context_set_error(struct yagl_gles_context *ctx, GLenum error);
 
 GLenum yagl_gles_context_get_error(struct yagl_gles_context *ctx);
 
+/*
+ * Allocate per-call memory, no need to free it.
+ */
+void *yagl_gles_context_malloc(struct yagl_gles_context *ctx, GLsizei size);
+void *yagl_gles_context_malloc0(struct yagl_gles_context *ctx, GLsizei size);
+
 struct yagl_gles_array
     *yagl_gles_context_get_array(struct yagl_gles_context *ctx, GLuint index);
 
index 08bcc38..edb6e38 100644 (file)
@@ -264,7 +264,7 @@ void yagl_host_glBufferData(GLenum target,
     }
 
     if (data_ && (size > 0)) {
-        data = g_malloc(size);
+        data = yagl_gles_context_malloc(ctx, size);
         if (!yagl_mem_get(ts,
                           data_,
                           size,
@@ -277,7 +277,6 @@ void yagl_host_glBufferData(GLenum target,
     yagl_gles_buffer_set_data(buffer_obj, size, data, usage);
 
 out:
-    g_free(data);
     yagl_gles_buffer_release(buffer_obj);
 }
 
@@ -312,7 +311,7 @@ void yagl_host_glBufferSubData(GLenum target,
         goto out;
     }
 
-    data = g_malloc(size);
+    data = yagl_gles_context_malloc(ctx, size);
     if (!yagl_mem_get(ts,
                       data_,
                       size,
@@ -327,7 +326,6 @@ void yagl_host_glBufferSubData(GLenum target,
     }
 
 out:
-    g_free(data);
     yagl_gles_buffer_release(buffer_obj);
 }
 
@@ -386,7 +384,7 @@ void yagl_host_glCompressedTexImage2D(GLenum target,
     YAGL_GET_CTX(glCompressedTexImage2D);
 
     if (data_ && (imageSize > 0)) {
-        data = g_malloc(imageSize);
+        data = yagl_gles_context_malloc(ctx, imageSize);
         if (!yagl_mem_get(ts,
                           data_,
                           imageSize,
@@ -407,7 +405,7 @@ void yagl_host_glCompressedTexImage2D(GLenum target,
                                          data);
 
 out:
-    g_free(data);
+    (void)0;
 }
 
 void yagl_host_glCompressedTexSubImage2D(GLenum target,
@@ -425,7 +423,7 @@ void yagl_host_glCompressedTexSubImage2D(GLenum target,
     YAGL_GET_CTX(glCompressedTexSubImage2D);
 
     if (data_ && (imageSize > 0)) {
-        data = g_malloc(imageSize);
+        data = yagl_gles_context_malloc(ctx, imageSize);
         if (!yagl_mem_get(ts,
                           data_,
                           imageSize,
@@ -447,7 +445,7 @@ void yagl_host_glCompressedTexSubImage2D(GLenum target,
                                             data);
 
 out:
-    g_free(data);
+    (void)0;
 }
 
 void yagl_host_glCopyTexImage2D(GLenum target,
@@ -515,7 +513,7 @@ void yagl_host_glDeleteBuffers(GLsizei n,
         goto out;
     }
 
-    buffer_names = g_malloc0(n * sizeof(*buffer_names));
+    buffer_names = yagl_gles_context_malloc0(ctx, n * sizeof(*buffer_names));
 
     if (buffers_) {
         if (!yagl_mem_get(ts,
@@ -545,7 +543,7 @@ void yagl_host_glDeleteBuffers(GLsizei n,
     }
 
 out:
-    g_free(buffer_names);
+    (void)0;
 }
 
 void yagl_host_glDeleteTextures(GLsizei n,
@@ -561,7 +559,7 @@ void yagl_host_glDeleteTextures(GLsizei n,
         goto out;
     }
 
-    texture_names = g_malloc0(n * sizeof(*texture_names));
+    texture_names = yagl_gles_context_malloc0(ctx, n * sizeof(*texture_names));
 
     if (textures_) {
         if (!yagl_mem_get(ts,
@@ -585,7 +583,7 @@ void yagl_host_glDeleteTextures(GLsizei n,
     }
 
 out:
-    g_free(texture_names);
+    (void)0;
 }
 
 void yagl_host_glDepthFunc(GLenum func)
@@ -686,7 +684,7 @@ void yagl_host_glDrawElements(GLenum mode,
         }
 
         if (indices_) {
-            indices = g_malloc(index_size * count);
+            indices = yagl_gles_context_malloc0(ctx, index_size * count);
             if (!yagl_mem_get(ts,
                               indices_,
                               index_size * count,
@@ -738,7 +736,6 @@ out:
                                    GL_ELEMENT_ARRAY_BUFFER,
                                    old_buffer_name);
     }
-    g_free(indices);
 }
 
 void yagl_host_glEnable(GLenum cap)
@@ -868,7 +865,7 @@ void yagl_host_glGetBooleanv(GLenum pname,
         goto out;
     }
 
-    params = g_malloc0(count * sizeof(*params));
+    params = yagl_gles_context_malloc0(ctx, count * sizeof(*params));
 
     if (!ctx->get_booleanv(ctx, pname, params)) {
         GLint param;
@@ -889,7 +886,7 @@ void yagl_host_glGetBooleanv(GLenum pname,
     }
 
 out:
-    g_free(params);
+    (void)0;
 }
 
 void yagl_host_glGetBufferParameteriv(GLenum target,
@@ -927,7 +924,7 @@ void yagl_host_glGetFloatv(GLenum pname,
         goto out;
     }
 
-    params = g_malloc0(count * sizeof(*params));
+    params = yagl_gles_context_malloc0(ctx, count * sizeof(*params));
 
     if (!ctx->get_floatv(ctx, pname, params)) {
         GLint param;
@@ -948,7 +945,7 @@ void yagl_host_glGetFloatv(GLenum pname,
     }
 
 out:
-    g_free(params);
+    (void)0;
 }
 
 void yagl_host_glGetIntegerv(GLenum pname,
@@ -964,7 +961,7 @@ void yagl_host_glGetIntegerv(GLenum pname,
         goto out;
     }
 
-    params = g_malloc0(count * sizeof(*params));
+    params = yagl_gles_context_malloc0(ctx, count * sizeof(*params));
 
     if (!ctx->get_integerv(ctx, pname, params)) {
         if (!yagl_get_integer(ctx, pname, params)) {
@@ -981,7 +978,7 @@ void yagl_host_glGetIntegerv(GLenum pname,
     }
 
 out:
-    g_free(params);
+    (void)0;
 }
 
 void yagl_host_glGetTexParameterfv(GLenum target,
@@ -1124,7 +1121,7 @@ void yagl_host_glTexImage2D(GLenum target,
             YAGL_SET_ERR(GL_INVALID_VALUE);
             goto out;
         }
-        pixels = g_malloc(stride * height);
+        pixels = yagl_gles_context_malloc(ctx, stride * height);
         if (!yagl_mem_get(ts,
                           pixels_,
                           stride * height,
@@ -1146,7 +1143,7 @@ void yagl_host_glTexImage2D(GLenum target,
                                pixels);
 
 out:
-    g_free(pixels);
+    (void)0;
 }
 
 void yagl_host_glTexParameterf(GLenum target,
@@ -1206,7 +1203,7 @@ void yagl_host_glTexSubImage2D(GLenum target,
             YAGL_SET_ERR(GL_INVALID_VALUE);
             goto out;
         }
-        pixels = g_malloc(stride * height);
+        pixels = yagl_gles_context_malloc(ctx, stride * height);
         if (!yagl_mem_get(ts,
                           pixels_,
                           stride * height,
@@ -1228,7 +1225,7 @@ void yagl_host_glTexSubImage2D(GLenum target,
                                   pixels);
 
 out:
-    g_free(pixels);
+    (void)0;
 }
 
 void yagl_host_glViewport(GLint x,
index 77766ce..4ab1340 100644 (file)
@@ -436,7 +436,7 @@ void yagl_host_glDeleteFramebuffers(GLsizei n,
         goto out;
     }
 
-    framebuffer_names = g_malloc0(n * sizeof(*framebuffer_names));
+    framebuffer_names = yagl_gles_context_malloc0(&ctx->base, n * sizeof(*framebuffer_names));
 
     if (framebuffers_) {
         if (!yagl_mem_get(gles2_api_ts->ts,
@@ -459,7 +459,7 @@ void yagl_host_glDeleteFramebuffers(GLsizei n,
     }
 
 out:
-    g_free(framebuffer_names);
+    (void)0;
 }
 
 void yagl_host_glDeleteProgram(GLuint program)
@@ -486,7 +486,7 @@ void yagl_host_glDeleteRenderbuffers(GLsizei n,
         goto out;
     }
 
-    renderbuffer_names = g_malloc0(n * sizeof(*renderbuffer_names));
+    renderbuffer_names = yagl_gles_context_malloc0(&ctx->base, n * sizeof(*renderbuffer_names));
 
     if (renderbuffers_) {
         if (!yagl_mem_get(gles2_api_ts->ts,
@@ -509,7 +509,7 @@ void yagl_host_glDeleteRenderbuffers(GLsizei n,
     }
 
 out:
-    g_free(renderbuffer_names);
+    (void)0;
 }
 
 void yagl_host_glDeleteShader(GLuint shader)
@@ -795,7 +795,7 @@ void yagl_host_glGetActiveAttrib(GLuint program,
     }
 
     if (bufsize > 0) {
-        name = g_malloc(bufsize);
+        name = yagl_gles_context_malloc(&ctx->base, bufsize);
     }
 
     yagl_gles2_program_get_active_attrib(program_obj,
@@ -826,7 +826,6 @@ void yagl_host_glGetActiveAttrib(GLuint program,
     }
 
 out:
-    g_free(name);
     yagl_gles2_program_release(program_obj);
 }
 
@@ -855,7 +854,7 @@ void yagl_host_glGetActiveUniform(GLuint program,
     }
 
     if (bufsize > 0) {
-        name = g_malloc(bufsize);
+        name = yagl_gles_context_malloc(&ctx->base, bufsize);
     }
 
     yagl_gles2_program_get_active_uniform(program_obj,
@@ -886,7 +885,6 @@ void yagl_host_glGetActiveUniform(GLuint program,
     }
 
 out:
-    g_free(name);
     yagl_gles2_program_release(program_obj);
 }
 
@@ -1126,7 +1124,7 @@ void yagl_host_glGetVertexAttribiv(GLuint index,
         goto out;
     }
 
-    params = g_malloc0(count * sizeof(*params));
+    params = yagl_gles_context_malloc0(&ctx->base, count * sizeof(*params));
 
     switch (pname) {
     case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
@@ -1161,7 +1159,7 @@ void yagl_host_glGetVertexAttribiv(GLuint index,
     }
 
 out:
-    g_free(params);
+    (void)0;
 }
 
 void yagl_host_glGetVertexAttribPointerv(GLuint index,
@@ -1370,7 +1368,7 @@ void yagl_host_glUniform1fv(GLint location,
     YAGL_GET_CTX(glUniform1fv);
 
     if (v_) {
-        v = g_malloc(count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           count * sizeof(*v),
@@ -1383,7 +1381,7 @@ void yagl_host_glUniform1fv(GLint location,
     ctx->driver_ps->Uniform1fv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform1i(GLint location,
@@ -1403,7 +1401,7 @@ void yagl_host_glUniform1iv(GLint location,
     YAGL_GET_CTX(glUniform1iv);
 
     if (v_) {
-        v = g_malloc(count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           count * sizeof(*v),
@@ -1416,7 +1414,7 @@ void yagl_host_glUniform1iv(GLint location,
     ctx->driver_ps->Uniform1iv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform2f(GLint location,
@@ -1437,7 +1435,7 @@ void yagl_host_glUniform2fv(GLint location,
     YAGL_GET_CTX(glUniform2fv);
 
     if (v_) {
-        v = g_malloc(2 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 2 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           2 * count * sizeof(*v),
@@ -1450,7 +1448,7 @@ void yagl_host_glUniform2fv(GLint location,
     ctx->driver_ps->Uniform2fv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform2i(GLint location,
@@ -1471,7 +1469,7 @@ void yagl_host_glUniform2iv(GLint location,
     YAGL_GET_CTX(glUniform2iv);
 
     if (v_) {
-        v = g_malloc(2 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 2 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           2 * count * sizeof(*v),
@@ -1484,7 +1482,7 @@ void yagl_host_glUniform2iv(GLint location,
     ctx->driver_ps->Uniform2iv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform3f(GLint location,
@@ -1506,7 +1504,7 @@ void yagl_host_glUniform3fv(GLint location,
     YAGL_GET_CTX(glUniform3fv);
 
     if (v_) {
-        v = g_malloc(3 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 3 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           3 * count * sizeof(*v),
@@ -1519,7 +1517,7 @@ void yagl_host_glUniform3fv(GLint location,
     ctx->driver_ps->Uniform3fv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform3i(GLint location,
@@ -1541,7 +1539,7 @@ void yagl_host_glUniform3iv(GLint location,
     YAGL_GET_CTX(glUniform3iv);
 
     if (v_) {
-        v = g_malloc(3 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 3 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           3 * count * sizeof(*v),
@@ -1554,7 +1552,7 @@ void yagl_host_glUniform3iv(GLint location,
     ctx->driver_ps->Uniform3iv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform4f(GLint location,
@@ -1577,7 +1575,7 @@ void yagl_host_glUniform4fv(GLint location,
     YAGL_GET_CTX(glUniform4fv);
 
     if (v_) {
-        v = g_malloc(4 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 4 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           4 * count * sizeof(*v),
@@ -1590,7 +1588,7 @@ void yagl_host_glUniform4fv(GLint location,
     ctx->driver_ps->Uniform4fv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniform4i(GLint location,
@@ -1613,7 +1611,7 @@ void yagl_host_glUniform4iv(GLint location,
     YAGL_GET_CTX(glUniform4iv);
 
     if (v_) {
-        v = g_malloc(4 * count * sizeof(*v));
+        v = yagl_gles_context_malloc(&ctx->base, 4 * count * sizeof(*v));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           v_,
                           4 * count * sizeof(*v),
@@ -1626,7 +1624,7 @@ void yagl_host_glUniform4iv(GLint location,
     ctx->driver_ps->Uniform4iv(ctx->driver_ps, location, count, v);
 
 out:
-    g_free(v);
+    (void)0;
 }
 
 void yagl_host_glUniformMatrix2fv(GLint location,
@@ -1639,7 +1637,7 @@ void yagl_host_glUniformMatrix2fv(GLint location,
     YAGL_GET_CTX(glUniformMatrix2fv);
 
     if (value_) {
-        value = g_malloc(2 * 2 * count * sizeof(*value));
+        value = yagl_gles_context_malloc(&ctx->base, 2 * 2 * count * sizeof(*value));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           value_,
                           2 * 2 * count * sizeof(*value),
@@ -1652,7 +1650,7 @@ void yagl_host_glUniformMatrix2fv(GLint location,
     ctx->driver_ps->UniformMatrix2fv(ctx->driver_ps, location, count, transpose, value);
 
 out:
-    g_free(value);
+    (void)0;
 }
 
 void yagl_host_glUniformMatrix3fv(GLint location,
@@ -1665,7 +1663,7 @@ void yagl_host_glUniformMatrix3fv(GLint location,
     YAGL_GET_CTX(glUniformMatrix3fv);
 
     if (value_) {
-        value = g_malloc(3 * 3 * count * sizeof(*value));
+        value = yagl_gles_context_malloc(&ctx->base, 3 * 3 * count * sizeof(*value));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           value_,
                           3 * 3 * count * sizeof(*value),
@@ -1678,7 +1676,7 @@ void yagl_host_glUniformMatrix3fv(GLint location,
     ctx->driver_ps->UniformMatrix3fv(ctx->driver_ps, location, count, transpose, value);
 
 out:
-    g_free(value);
+    (void)0;
 }
 
 void yagl_host_glUniformMatrix4fv(GLint location,
@@ -1691,7 +1689,7 @@ void yagl_host_glUniformMatrix4fv(GLint location,
     YAGL_GET_CTX(glUniformMatrix4fv);
 
     if (value_) {
-        value = g_malloc(4 * 4 * count * sizeof(*value));
+        value = yagl_gles_context_malloc(&ctx->base, 4 * 4 * count * sizeof(*value));
         if (!yagl_mem_get(gles2_api_ts->ts,
                           value_,
                           4 * 4 * count * sizeof(*value),
@@ -1704,7 +1702,7 @@ void yagl_host_glUniformMatrix4fv(GLint location,
     ctx->driver_ps->UniformMatrix4fv(ctx->driver_ps, location, count, transpose, value);
 
 out:
-    g_free(value);
+    (void)0;
 }
 
 void yagl_host_glUseProgram(GLuint program)